Skip to main content
July 24, 2010
Answered

First letter of String to capital

  • July 24, 2010
  • 3 replies
  • 2837 views

Hi.  Trying to get the first letter of a String to capital, and the rest of the String must be lower case.  I think I am on the right tracks with the first letter, but not sure how to make everything else forced to be lowercase.

Heres a bit of info because I might need to change things. 
I load in a flashvar vaiable by

var flashVar3  = root.loaderInfo.parameters.myVar2;

I am not to sure what data type this would be, not sure what flashvars comes as.  I am then doing

var oldStr:String = flashVar3;
var newStr:String = oldStr.charAt(0).toUpperCase() + oldStr.substr(1);

tf3.text=" "+ newStr;

I dont know if the first line is an issue, because i dont know if flashvars comes as a String.  But what I have generally would work on the first letter. Not to sure how I can force the rest of the String to be lowercase though.


At this moment in time, I get the error

TypeError: Error #1010: A term is undefined and has no properties.

But I am not sure if this is because my flashvars variable is defined at runtime.  Any advise on getting all of this working would be great.

cheers

This topic has been closed for replies.
Correct answer Ned Murphy

And while you're trying to figure out and get Andrei's version working, here's the solution I was referring to which primarily relies on String/Array methods and builds on what you already had working... complete with certain words being ignored... I won't make claim as to either approach being best/better though, just different approaches...

var str:String = "tHiS IS a sTrInG tO Be caPiTalIzed";

var ignoreArray = ["the","is","a","an","to"];

var strArray = str.toLowerCase().split(" ");

for(var i:int=0; i<strArray.length; i++){
      if(ignoreArray.indexOf(strArray) == -1){
            strArray = strArray.charAt(0).toUpperCase() + strArray.substr(1);
      }
}

var newStr:String = strArray.join(" ");
trace(newStr);

3 replies

Inspiring
July 25, 2010

Here is a code that capitalizes sentence or does it with filtering(not capitalizing) some words:

var originalString:String = "tHiS IS a sTrInG tO Be caPiTalIzed";
trace(capitalize(originalString));
trace(titleCase(originalString));

function capitalize(str:String):String {
     var re:RegExp = /\b[a-z]/g;
     return str.toLowerCase().replace(re, capitalizeWord);
}

function capitalizeWord(...args):String {
     return args[0].toUpperCase();
}

function titleCase(str:String):String {
     return str.toLowerCase().replace(/\b([a-z])(\w*)\b/g, cpaitalizeFilter);
}
     
function cpaitalizeFilter(...args):String {
     var re:RegExp = /^(a|about|after|an|and|at|by|for|from|in|is|into|nor|of|on|onto|over|the|to|up|with|within)$/;
     if (re.test(args[0]) && args[args.length - 2])
          return args[0];
     else
          return args[1].toUpperCase() + args[2];
}

Inspiring
July 25, 2010

To get you started with regular expressions, if you wish, here is an example on how to capitalize string:

var re:RegExp = /\b[a-z]/g;
var str:String = "tHiS IS a sTrInG tO Be caPiTalIzed";
str = str.toLowerCase();
str = str.replace(pat, capitalize);
trace(str);

function capitalize(...arg):String {
     return args[0].toUpperCase();
}

If I have time I will post code that has conditional capitalization that excludes certain words.

Ned Murphy
Legend
July 24, 2010

If you first convert the entire string to lower case, then you can change the first character and be done with it...

var oldStr:String = flashVar3.toLowerCase();

July 24, 2010

Kool.  This one would be a bit trickier, but how would I go about doing it.  How would I only allow the first letter of every word to be capital, but only if the user specifies it to be so?  So if they done

My NAmE is NiCk

it would be

My Name is Nick

Ned Murphy
Legend
July 24, 2010

You'd have to make it specially-intelligent to have it ignore capitalizing the "is", but what you can do is split() the string using the spaces between words as the delimiter, then take each string in the resulting array and do what you did in your first posting... then join the words back into a string placing spaces between each as you build it.

You should look up the String class and go thru all of the methods and properties available for it.