How can i get the first letter of this string

  • Follow


Hello, i'm a newbie. I have a question that how can i get the first
letter of this string: "=C3=A1 is the first letter".
my solution:

>> string =3D "=C3=A1 is the first letter"
>> str =3D string[0..1] # [0..1] because =C3=A1 =3D 2byte

But in many case i don't know the size of the first letter.
Any help would be much appreciated.

-- =

Posted via http://www.ruby-forum.com/.=

0
Reply minhduct4 (3) 3/3/2011 3:41:57 AM

You can use the /u flag for a regex to signal utf-8 matching...but you 
didn't mention what encoding your strings are in.  The regex would look 
like this:

"some string" =~ /^(.)/u
puts $1

You also didn't mention what version of ruby you are using...ruby 1.8 or 
1.9.

-- 
Posted via http://www.ruby-forum.com/.

0
Reply 7stud 3/3/2011 3:52:37 AM


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

irb(main):023:0> bacon="chunky"
=> "chunky"
irb(main):024:0> bacon[0]
=> 99
irb(main):025:0> bacon[0].chr
=> "c"

99 is the ascii number that represents the letter 'c'.

I think that explains what you want.

-- 
Thiago Fernandes Massa
11 83979414

0
Reply Thiago 3/3/2011 5:32:38 AM

That won't work on Ruby 1.9, as bacon[0] will return the first actual =
character. This technique
works on both:

>> bacon =3D "chunky"
=3D> "chunky"
>> bacon[0,1]
=3D> "c"

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

On Mar 3, 2011, at 12:32 AM, Thiago Massa wrote:

> irb(main):023:0> bacon=3D"chunky"
> =3D> "chunky"
> irb(main):024:0> bacon[0]
> =3D> 99
> irb(main):025:0> bacon[0].chr
> =3D> "c"
>=20
> 99 is the ascii number that represents the letter 'c'.
>=20
> I think that explains what you want.
>=20
> --=20
> Thiago Fernandes Massa
> 11 83979414


0
Reply Michael 3/3/2011 5:54:12 AM

thanks for your  help!
My strings are in UTF-8 encoding. Ruby version 1.8.7

I just follow your instructions and got the first letter of that string. =

But i still not get the remainder of string after get the first letter.

My problem now is convert the UTF-8 string to ansi string.
ex: purpose: change "=C3=A2=C3=AA=C6=A1=C3=AD=C5=A9" to "aeoiu".
I really confuse and don't have any way to do that.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GVkIHZpYSBodHRwOi8vd3d3LnJ1YnktZm9ydW0uY29tLy4NCiAJCSAJICAgCQkgIA==

0
Reply tdihp (3) 3/3/2011 7:58:48 AM

On Thu, Mar 3, 2011 at 5:32 AM, Thiago Massa <thiagown@gmail.com> wrote:

> irb(main):023:0> bacon=3D"chunky"
> =3D> "chunky"
> irb(main):024:0> bacon[0]
> =3D> 99
> irb(main):025:0> bacon[0].chr
> =3D> "c"
>
> 99 is the ascii number that represents the letter 'c'.
>
> I think that explains what you want.
>

On Thu, Mar 3, 2011 at 5:54 AM, Michael Edgar <adgar@carboni.ca> wrote:

> That won't work on Ruby 1.9, as bacon[0] will return the first actual
> character. This technique
> works on both:
>
> >> bacon =3D "chunky"
> =3D> "chunky"
> >> bacon[0,1]
> =3D> "c"
>

This will not work on Ruby 1.8, because the character involved is a two-byt=
e
character, and before 1.9 there's nothing but bytes.

s =3D "=C3=A1"
s.inspect #=3D> =3D> "\303\241"
s[0] #=3D> "\303"
s[0, 1] #=3D> "\303"
s[0].chr #=3D> "\303"

Since you're on 1.8.7, you could use

$KCODE =3D "U" # UTF-8
s.chars.first #=3D> "=C3=A1"

Note that if you do not set $KCODE, you will just get "\303".

0
Reply mentionuse (21) 3/3/2011 10:27:57 AM

duc nguyen wrote in post #985120:
> thanks for your  help!
> My strings are in UTF-8 encoding. Ruby version 1.8.7
>
> I just follow your instructions and got the first letter of that string=
0
Reply bbxx789_05ss (1895) 3/4/2011 12:17:27 AM

thanks for all!
I tried all instructions above and they work fine when i run from =

command line. But when i insert them to my .rb file in my project and =

debug, An error has happen, and i don't know why

"=C3=A1 is the first letter" =3D~ /^(.)(.*)/u
puts "-->#{$1}<--"
puts "-->#{$2}<--"

--output in command prompt window:--
-->=C3=A1<--
--> is the first letter<--

But in the log file of my project
I 03/04/2011 09:07:39:169 b0353000  APP| -->=E2=88=9A<--
I 03/04/2011 09:07:39:169 b0353000  APP| -->=C2=B0 is the first letter<--=


Again, i change the code:

"=C3=A1 is the first letter" =3D~ /^(..)(.*)/u
puts "-->#{$1}<--"
puts "-->#{$2}<--"

And now it works.
I 03/04/2011 09:11:03:414 b03d5000   APP| -->=C3=A1<--
I 03/04/2011 09:11:03:414 b03d5000   APP| --> is the first letter<--

My project is a rhodes project. Rhodes version 2.2.6. Ruby 1.8.7.

-- =

Posted via http://www.ruby-forum.com/.=

0
Reply minhduct4 (3) 3/4/2011 2:16:32 AM

Wow...that's strange: when I run my example, no log file is created.  I 
wonder why that is???

In other words, you are running some extra code that writes to a file, 
and you want us to troubleshoot that code without seeing it?  O...kay. 
Your statement that writes to the file takes one byte of the two bytes 
making up the and writes the single byte to the file, which is then 
interpreted as gibberish by whatever application you are using to view 
the file.

-- 
Posted via http://www.ruby-forum.com/.

0
Reply bbxx789_05ss (1895) 3/4/2011 3:11:29 AM

9 Replies
531 Views

(page loaded in 0.13 seconds)

Similiar Articles:













7/23/2012 1:45:40 PM


Reply: