Erlang/OTP Forums

Author Message

<  Erlang  ~  How to break a string

tharindufit
Posted: Wed Sep 23, 2009 5:04 am Reply with quote
User Joined: 23 Sep 2009 Posts: 18 Location: Colombo, SriLanka
Hi,

I am new to erlang and just wanted to clarify sth about string handling. I want to break a string and print the broken part. could someone kindly point some code fragment to do this.

Code:
-define(CONST,"abcdefg").

%% I Want to break CONST from 4th index and print
%% rest like (e.g. fg ) How can I do this


When I try to print a string its printed as a list [65,66] something. How can I print the ascii characters instead of this.

Thank you,

Tharindu Madushanka
tharindufit.wordpress.com
View user's profile Send private message Yahoo Messenger
uwiger
Posted: Wed Sep 23, 2009 7:11 am Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
tharindufit wrote:

Code:
-define(CONST,"abcdefg").

%% I Want to break CONST from 4th index and print
%% rest like (e.g. fg ) How can I do this


lists:nthtail(3, ?CONST)

nthtail takes the tail of the nth element, so if you want the 4th element to be part of the printed list, you need to decrement by one e.g. nthtail(0,?CONST) would print the entire string.


tharindufit wrote:
When I try to print a string its printed as a list [65,66] something. How can I print the ascii characters instead of this.


Strings are lists of ASCII codes in Erlang.
You can use io:format("~s", [String]) if you know it's a string. If you use io:format("~p", [String]), it will be pretty-printed, which among other things means that it will print as a string if it's a list of integers that all correspond to printable ASCII codes; otherwise it will be printed as a list. This is what happens in the Erlang shell.
View user's profile Send private message Visit poster's website
tharindufit
Posted: Wed Sep 23, 2009 9:19 am Reply with quote
User Joined: 23 Sep 2009 Posts: 18 Location: Colombo, SriLanka
Thank you for replying to the post :)

And I saw there is also a string module in the documentation, it might also be useful I will check it and ask if I get any more issues
View user's profile Send private message Yahoo Messenger

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum