function ConvertToUpperCase()
            {
            var result;
            var numaric = "sonia";
            var Character = numaric.charAt(0);
            result=Character.toUpperCase(); 
              //In Result char is coming in UpperCase
              /[B]/I want how to replace the charater at postion 0 in numaric with result[/B]
            }

Recommended Answers

All 6 Replies

Hi sonia,

you could try this:

function ConvertToUpperCase() {
var result;
var numaric = "sonia";
var Character = numaric.charAt(0);
result = numaric.replace( Character, Character.toUpperCase() ); // Converting target character to uppercase letter.
}

I would try something like:

function ConvertToUpperCase()
            {
            var result;
            var numaric = "sonia";
            result=numaric.charAt(0).toUpperCase() + numaric.substring(1); 
            }

This changes the first character to upper case and then adds the second and subsequent character to the end.

HTH

Stoo

--
<FAKE SIGNATURE>

That will only work if you the 1st spot of character. What if she tries to point it out at Character = numaric.charAt( 2 ); ? Of course it will be needing some workaround that includes looping process, which will keep the line a bit longer compare to the used of the replacement method. If you are building programs, try to think what is on up ahead, not just things that you can do right now...

-essential

THX BOTH OF U
Working Code

protected void Page_Load(object sender, EventArgs e)
    {
           try
           {
             if (!IsPostBack)
             {
           txtCompanyName.Attributes.Add("onblur", "javascript :  ConvertToUpperCase()");

 }
           }
           catch (Exception  ex)
           {
               lblStatus .Text =ex.Message .ToString ();
           }
    }


 function ConvertToUpperCase()
            {
            var numaric = document.getElementById ("txtCompanyName").value;
            numaric =numaric.charAt(0).toUpperCase() + numaric.substring(1);
            var pos = numaric.indexOf(" ");
            while(pos > -1)
                {
                  numaric =numaric.substring(0,pos+1) +  numaric.charAt(pos+1).toUpperCase() + numaric.substring(pos+2);
                  pos = numaric.indexOf(" ", pos+1);
                }
            document.getElementById ("txtCompanyName").value=numaric;
           }

Suppose i type miss sonia sardana in textbox when the control loss focus or when we press tab.output is Miss Sonia Sardana..Although its long process,because there is no way to replace character at a particular position in string...any better ideas are welcome?

That will only work if you the 1st spot of character. What if she tries to point it out at Character = numaric.charAt( 2 ); ? Of course it will be needing some workaround that includes looping process, which will keep the line a bit longer compare to the used of the replacement method. If you are building programs, try to think what is on up ahead, not just things that you can do right now...

-essential

It depends on the intention of the function. Both of our code samples give the same result if numaric="sonia". If numaric="this is some text" then your function would produce "This is some TexT" while mine would produce "This is some text". If the desired outcome is to convert all instances of the first character to upper case then your function would be the right one for the situation.

--
<FAKE SIGNATURE>

Sonia,

You might like to try something like this:

//This is a general utility function that returns an Initial Caps version of the input string
function initialCaps(str){
	var a = str.split(' ');
	for(var i=0; i<a.length; i++){
		ch = a[i].charAt(0);
		a[i] = a[i].replace(ch, ch.toUpperCase());
	}
	return a.join(' ');
}

//This function is applies initialCaps() to a specified form field.
function field2InitialCaps(filedId){
	var f = document.getElementById (filedId);
	if(f) { f.value = initialCaps(f.value); }
}
<!-- Demo -->
<input id="txtCompanyName" type="text" value="sonia is a beautiful name" size="50" /><br />
<a href="" onclick="field2InitialCaps('txtCompanyName');return false">Convert to Initial Caps</a>

Airshow

commented: Nice execution, simple and elegant... +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.