string:strip/3 don't work right striping

classic Classic list List threaded Threaded
3 messages Options
shk
Reply | Threaded
Open this post in threaded view
|

string:strip/3 don't work right striping

shk
Hello,

I receive data by socket in tcp:

receive_loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
         {ok, Data} ->
            ReParseData = string:strip(string:to_lower(Data), both, $.),
            io:format(ReParseData, []),
            case ReParseData of
                "quit\r\n" ->
                  gen_tcp:send(Socket, "quit \r\n"),
                  gen_tcp:close(Socket);
                _ ->
                  io:format(Data, []),
                  receive_loop(Socket)
             end;
         {error, closed} ->
            ok
    end.

if I send .........................Hello ......................................
I got in shell:
>Hello ......................................

How can i correctly delete space from left and right in stirng?

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: string:strip/3 don't work right striping

Gustav Simonsson-2

string:strip(string:to_lower("....Hello...."), both, $.).
will evaluate to "hello". The reason you received dots in your
shell might be because you print out the raw Data after you print
out ReParseData, without anything printed between.

Best Regards,
Gustav Simonsson

----- Original Message -----
From: "shk" <[hidden email]>
To: [hidden email]
Sent: Wednesday, 19 January, 2011 07:48:54 GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna
Subject: [erlang-questions] string:strip/3 don't work right striping


Hello,

I receive data by socket in tcp:

receive_loop(Socket) ->
    case gen_tcp:recv(Socket, 0) of
         {ok, Data} ->
            ReParseData = string:strip(string:to_lower(Data), both, $.),
            io:format(ReParseData, []),
            case ReParseData of
                "quit\r\n" ->
                  gen_tcp:send(Socket, "quit \r\n"),
                  gen_tcp:close(Socket);
                _ ->
                  io:format(Data, []),
                  receive_loop(Socket)
             end;
         {error, closed} ->
            ok
    end.

if I send .........................Hello
......................................
I got in shell:
>Hello ......................................

How can i correctly delete space from left and right in stirng?

Thank you.
--
View this message in context: http://erlang.2086793.n4.nabble.com/string-strip-3-don-t-work-right-striping-tp3224632p3224632.html
Sent from the Erlang Questions mailing list archive at Nabble.com.

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]


________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: string:strip/3 don't work right striping

Bengt Kleberg
In reply to this post by shk
Greetings,

While I can not see all space that you want to delete in the input (it
would be helpful if you provided " at the beginning and the end) I can
guess that you have two \n at the end of each line. If you want to
delete any leading and/or trailing blanks (space) then string:strip/1
would be a good choice.
Given a better description of the input, and what you want to do with,
it might result in a better suggestion.


bengt

On Wed, 2011-01-19 at 07:48 +0100, shk wrote:

> Hello,
>
> I receive data by socket in tcp:
>
> receive_loop(Socket) ->
>     case gen_tcp:recv(Socket, 0) of
> {ok, Data} ->
>    ReParseData = string:strip(string:to_lower(Data), both, $.),
>    io:format(ReParseData, []),
>    case ReParseData of
> "quit\r\n" ->
>          gen_tcp:send(Socket, "quit \r\n"),
>  gen_tcp:close(Socket);
>        _ ->
>  io:format(Data, []),
>  receive_loop(Socket)
>     end;
> {error, closed} ->
>    ok
>     end.
>
> if I send .........................Hello
> ......................................
> I got in shell:
> >Hello ......................................
>
> How can i correctly delete space from left and right in stirng?
>
> Thank you.


________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]