Replace the 1st Character in every word

G

Guest

I am new to this .net environement. I'm needing to do a simple task - I
need to read the first character of a word and replace it with an uppercase.
Example:

"horse" --> change to "Horse"

I can do this on an as/400 using RPG, but cannot figure out the syntax to do
it in .net with the .substring and .toupper methods.
 
W

William F. Robertson, Jr.

The easiest suggestion I could make it so split off a space make the first
letter caps, then rejoin adding the space back. This probably isn't the
most performant way to handle this, so don't use this in a tight loop, but
it does show some of the built in string functions and char arrays.

bill

string s = "hello world, dot net rocks!";
string [] sa = s.Split( ' ' );
string word = null;
for( int i = 0 ; i < sa.Length ; i++ )
{
word = sa;
char [] letters = word.ToCharArray();
if ( letters.Length > 0 )
{
letters[0] = Char.ToUpper( letters[0] );
}
word = new string( letters );
sa = word;
}
s = String.Join( " ", sa );
Console.Write( s );
 
K

Kikoz

Hi.

string original = "horse";

string result = original.Substring(0,1).ToUpper()+original.Remove(0,1);
 
G

Guest

Thank you so much. Works like a champ.

jrmc

Kikoz said:
Hi.

string original = "horse";

string result = original.Substring(0,1).ToUpper()+original.Remove(0,1);
 
A

Amar

Even though i cannot understand you want to do that here is a sample.

string str = "horse";
string newstr = str.Replace(str.Substring(0,1),str.Substring(0,1).ToUpper());
 
G

Guest

string orig="a horse is a horse";
string
capped=System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(orig);
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top