Game Development Community

Capitalize the first char in a string

by Kyle Cook · in Torque Game Engine · 06/18/2006 (4:24 pm) · 2 replies

This will be used for my character naming, my question is how do you capitalize the first letter in a string? I will show you what I am trying to do here.. I am a visual person myself, it is sad that some of us see code as "visual" lol..

function ValidateCharacterName(%name)
{
   %name=stripTrailingSpaces(%name);
   %name=stripChars(%name,"1234567890-=!@#$%^&*()_+\][|}{';/.,:?><'~\"");
   %name=strlwr(%name);
   %name=stripMLControlChars(%name);
   if(containsBadWords(%name)
      return false;
   %first=getSubStr(%name,0,1);
   %newfirst=strupr(%first);
   %name=strreplace(%name,%first,%newfirst);  [i]//This won't work because it replaces all of the characters with the value of %first with %newfirst [/i]
   $pref::Player::CharacterName=%name;
}

#1
06/18/2006 (5:05 pm)
You can try using the function getSubStr, like you were doing...

So using your %name variable, you could extract the first character to one string, and the rest of the characters to another string (using strlen to figure out how many characters that is). Then use strupr to convert the first character to upper case, and then just concatenate the two back together.

There might be an easier way, though...
#2
06/18/2006 (5:07 pm)
Oh my, my brain wen't dead for a bit, I totally forgot about string concatenation. Thanks!