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]
            }

Dani AI

Generated

JavaScript strings are immutable, so you cannot change a character in place — you must build and return a new string. That explains why 's replace-by-character can be surprising (it changes every matching character, not a single index), while and show the common patterns for first-letter or word-by-word initial-caps. Below are compact, modern helpers that avoid the common pitfalls (wrong indexes, surrogate pairs, locale rules) and can be dropped into a form-processing workflow.

// replace a code-point at a given position (Array.from handles surrogate pairs)
function replaceAt(str, index, replacement) {
  const chars = Array.from(str);
  if (index < 0 || index >= chars.length) return str;
  chars[index] = replacement;
  return chars.join('');
}

// uppercase the grapheme at index (Intl.Segmenter if available, fallback to Array.from)
function uppercaseAtGrapheme(str, index, locale) {
  if (typeof Intl !== 'undefined' && Intl.Segmenter) {
    const seg = new Intl.Segmenter(locale, { granularity: 'grapheme' });
    const parts = Array.from(seg.segment(str), s => s.segment);
    if (index < 0 || index >= parts.length) return str;
    parts[index] = parts[index].toLocaleUpperCase(locale);
    return parts.join('');
  }
  const chars = Array.from(str);
  if (index < 0 || index >= chars.length) return str;
  chars[index] = chars[index].toLocaleUpperCase(locale);
  return chars.join('');
}

Notes and troubleshooting: use toLocaleUpperCase(locale) for language-sensitive casing (Turkish i). For title-casing whole names prefer segmenting on word boundaries (Intl.Segmenter or a Unicode-aware regex) so hyphenated names and apostrophes behave correctly. Guard empty strings and out-of-range indexes. For simple first-letter-only edits the classic substring/concat works, but for robust, future-proof code (handling emoji, accents, and locales) prefer the approaches above. These choices reconcile the suggestions from , , and while avoiding accidental global replacements.

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.