|
|
| Author |
Message |
< Erlang ~ How to break a string |
| tharindufit |
Posted: Wed Sep 23, 2009 5:04 am |
|
|
|
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 |
|
|
| Back to top |
|
| uwiger |
Posted: Wed Sep 23, 2009 7:11 am |
|
|
|
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. |
|
|
| Back to top |
|
| tharindufit |
Posted: Wed Sep 23, 2009 9:19 am |
|
|
|
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 |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|