Replace the 1st Character in every word

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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 );
 
Hi.

string original = "horse";

string result = original.Substring(0,1).ToUpper()+original.Remove(0,1);
 
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());
 
string orig="a horse is a horse";
string
capped=System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(orig);
 
Back
Top