Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

substr + utf

by b888 (Beadle)
on Aug 18, 2005 at 16:01 UTC ( [id://484841]=perlquestion: print w/replies, xml ) Need Help??

b888 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings.

What is the best way to make substr on strings, that contain utf8(16/32) symbols? So "substr"ed part will contain valid utf symbols after this operation.

Thanks

Replies are listed 'Best First'.
Re: substr + utf
by halley (Prior) on Aug 18, 2005 at 16:29 UTC
    Until Perl knows that a given scalar intentionally contains utf8-encoded characters, it may not act on that assumption. See Encode and perldoc perlunicode.

    --
    [ e d @ h a l l e y . c c ]

Re: substr + utf
by ikegami (Patriarch) on Aug 18, 2005 at 16:09 UTC
    substr works on characters, not bytes, so you don't have to do anything special.

      Made several tests before posting - at least on my PC it works with utf8 symbols as if it is 2 chars.
      Maybe it depends on some OS settings?

        Are you sure the string is actually unicode? I think you interpreted every byte of your input as a character instead of interpreting your input as a unicode string. I'm not sure how to convert from one to the other.

        Maybe it depends on some OS settings?

        I doubt it. Try the following code.

        # $str = Approximation of "Eric" in Katakana. $str = "\x{30A8}\x{30FC}\x{30EA}\x{30AF}"; print(length($str), "\n"); # 4 $ch = substr($str, 0, 1); print(($ch eq "\x{30A8}" ? "equal" : "not equal"), "\n"); # equal

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://484841]
Approved by Nevtlathiel
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found