|
[jruby-user] Iterating over characters in a String ?: msg#00142lang.jruby.user
I'm looking for some way to iterate over the characters of a String in Ruby. Ruby's class String does not provide a method each_char, nor does JRuby's class String. Instead, both provide method each_byte. Now, Ruby does not do Unicode. What a shame for a language developed in Japan ! Instead, it believes that the size of a char is 8 bits. Thus, Ruby thinks that Matz' name, which is 松本行弘, has 12 characters: matz = '松本行弘' matz.length -> 12 Help. How do I translate the following Java method into JRuby ? void m(String s) { for (int i = 0; i < s.length(); i++) { System.out.println(s.charAt(i)); } } The following does *not* work in Ruby, because it outputs each byte, instead of each letter. def each_char(str) 0...str.length do |i| puts str[i] end end |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | [jruby-user] Re: jirb and Netbeans Matisse: 00142, Tacio |
|---|---|
| Next by Date: | RE: [jruby-user] Iterating over characters in a String ?: 00142, Peter K Chan |
| Previous by Thread: | [jruby-user] Re: jirb and Netbeans Matissei: 00142, Tacio |
| Next by Thread: | RE: [jruby-user] Iterating over characters in a String ?: 00142, Peter K Chan |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |