Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split a string

Status
Not open for further replies.

christioan

Programmer
May 1, 2002
7
0
0
AT
How can I split a string?
For example:
'123 abc 456'
And now I want to split this string at the SPACE-Signs into 3 strings and save them in 3 different variables!

Thanks!
 
Hello.
When using Amzi, it describes as follows.

?- string_split($123 abc 456$, $ $, S).
S = [$123$,$abc$,$456$] ;
no

?- string_split($123 abc 456$, $ $, [S1, S2, S3]).
S1 = $123$
S2 = $abc$
S3 = $456$ ;
no

Probably, it is dependent on a compiler.
"string_split" is the predicate included in Amzi.
 
Have you ever tried to realize the "splitting-problem" in SWI-Prolog? I tried your tip, but this version of prolog doesn't have the predicate "string_split"!!
In every other programming-language I would search the "space-signs" in the string and copy the parts infront of the sign in another string, but how can I do this in SWI-Prolog????

thx
 
Hello.
The SWI has "string_to_list" predicate.

string_to_list('123 abc 456', X).
X = [49, 50, 51, 32, 97, 98, 99, 32, 52|...]

It can also convert char-list to string.

string_to_list(X, [49, 50, 51]).
X = '123'

So, how about this idea? -> (1) convert string to char-list, (2)search 32 in the char-list and copy the parts infront of it, (3) return char-list to string.
 
Hello!
Good idea! I had nearly the same idea, but than I get a problem! The first part is clear, but how can I return the char-list between the first and the second space-sign? Perhaps it's easy, but I don't know if it will be possible to save variables in Prolog!!!
Thx for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top