×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

split a string

split a string

split a string

(OP)
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!

RE: split a string

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.

RE: split a string

(OP)
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

RE: split a string

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.

RE: split a string

(OP)
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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login