Splitting a string into characters - not bytes

P

Peter Hickman

I realise that this is probably a known thing but my google-fu is not
working today.

I am learning Japanese and I wanted to write an application (probably
a web app) that I can paste some Japanese into and highlight the
characters that I should know. To do this I need to take a string of
Japanese, such as "$BL\$,3P$a$F$7$^$C$?!#L2$$$N$K!D!#(B" and break it down into
characters, that is this string contains 16 characters and a space
(despite being 49 bytes long).

Of course some strings also contain latin characters mixed in, such as
"$B26$N2G$H2qOC#2(B | $B$"$M$3(B".

My home-brew solutions always seem to screw up, but then I thought.
This is Ruby! Why the heck am I reinventing the wheel?

So can someone clue me in on how to take a string of Japanese and
break it up into characters?

Please
 
A

Ammar Ali

MjAxMC8xMS80IFBldGVyIEhpY2ttYW4gPHBldGVyaGlja21hbjM4NkBnb29nbGVtYWlsLmNvbT46
Cj4gU28gY2FuIHNvbWVvbmUgY2x1ZSBtZSBpbiBvbiBob3cgdG8gdGFrZSBhIHN0cmluZyBvZiBK
YXBhbmVzZSBhbmQKPiBicmVhayBpdCB1cCBpbnRvIGNoYXJhY3RlcnM/Cgo+PiAi5L+644Gu5auB
44Go5Lya6Kmx77ySIHwg44GC44Gt44GTIi5zY2FuKC8uL3UpCj0+IFsi5L+6IiwgIuOBriIsICLl
q4EiLCAi44GoIiwgIuS8miIsICLoqbEiLCAi77ySIiwgIiAiLCAifCIsICIgIiwgIuOBgiIsICLj
ga0iLCAi44GTIl0KClJlZ2FyZHMsCkFtbWFyCg==
 
P

Peter Hickman

Thank you thank you thank you

Damn that was a simple fix, not to mention a very fast response

Thank you again
 
S

Stefano Crocco

|I realise that this is probably a known thing but my google-fu is not
|working today.
|
|I am learning Japanese and I wanted to write an application (probably
|a web app) that I can paste some Japanese into and highlight the
|characters that I should know. To do this I need to take a string of
|Japanese, such as "$BL\$,3P$a$F$7$^$C$?!#L2$$$N$K!D!#(B" and break it down into
|characters, that is this string contains 16 characters and a space
|(despite being 49 bytes long).
|
|Of course some strings also contain latin characters mixed in, such as
|"$B26$N2G$H2qOC#2(B | $B$"$M$3(B".
|
|My home-brew solutions always seem to screw up, but then I thought.
|This is Ruby! Why the heck am I reinventing the wheel?
|
|So can someone clue me in on how to take a string of Japanese and
|break it up into characters?
|
|Please

What version of ruby are you using? In ruby 1.9, you can simply use
String#each_char:

"$BL\$,3P$a$F$7$^$C$?!#L2$$$N$K!D!#(B".each_char{|c| puts c}
=>
$BL\(B
$B$,(B
$B3P(B
$B$a(B
$B$F(B
$B$7(B
$B$^(B
$B$C(B
$B$?(B
$B!#(B
$BL2(B
$B$$(B
$B$N(B
$B$K(B
$B!D(B
$B!#(B

You may need to set the encoding appropriately, however.

I don't know how you'd do it in ruby 1.8.

I hope this helps

Stefano
 
A

Ammar Ali

Thank you thank you thank you

Damn that was a simple fix, not to mention a very fast response

Thank you again

I was just reminded of that simple solution myself yesterday on this very list.

Cheers,
Ammar
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

If you want the array you can do str.chars.to_a.
 
A

Ammar Ali

If you want the array you can do str.chars.to_a.

It's worth noting that String#chars is not available in all versions of ruby:
NoMethodError: undefined method `chars' for "str":String
from (irb):2

Regards,
Ammar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top