How to convert letters to uppercase?

R

Richard Sandoval

Im unfamiliar with regular expressions.

How would I be able to change all letters to uppercase to the first
period?

Before:

helpuser01.hello.txt


After:

HELPUSER01.hello.txt


Any advice would be dearly appreciated.
 
B

Brian Candler

Richard Sandoval wrote in post #976947:
Im unfamiliar with regular expressions.

How would I be able to change all letters to uppercase to the first
period?

Before:

helpuser01.hello.txt


After:

HELPUSER01.hello.txt

1. Write a regular expression to match everything before the first
period.

2. Substitute it with the uppercased version

str = "helpuser01.hello.txt"
str.sub!(/\A.*?\./) { $&.upcase }
puts str
 
M

Martin DeMello

Im unfamiliar with regular expressions.

How would I be able to change all letters to uppercase to the first
period?

Before:

helpuser01.hello.txt


After:

HELPUSER01.hello.txt

ruby-1.9.2-p0 > a = "helpuser01.hello.txt"
=> "helpuser01.hello.txt"
ruby-1.9.2-p0 > a.sub(/.*?\./) {|m| m.upcase}
=> "HELPUSER01.hello.txt"

martin
 
T

Tadeusz Bochan

Perhaps much less efficient, but 'easier' for newbie :
a="helpuser01.hello.txt"
a=a.split(".",2)
a[0]=a[0].upcase
a=a.compact.join(".")


Martin DeMello wrote in post #976951:
 
Y

Y. NOBUOKA

Hi, Richard

When the string doesn't contain a period, you want to change all
letters to uppercase? or not?
If you want to do, the following code is not good for you:
str.sub!(/\A.*?\./) { $&.upcase }

str = "hello"
str.sub!(/\A.*?\./) { $&.upcase }
puts str
# hello

Please use the following regexp instead:

str.sub!( /\A[^\.]*/ ) { |s| s.upcase }

str = "hello"
str.sub!( /\A[^\.]*/ ) { |s| s.upcase }
puts str
# HELLO

Regards,
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top