943,920 Members | Top Members by Rank

Ad:
Aug 2nd, 2009
0

Replace character at a particular position

Expand Post »
function ConvertToUpperCase()
            {
            var result;
            var numaric = "sonia";
            var Character = numaric.charAt(0);
            result=Character.toUpperCase(); 
              //In Result char is coming in UpperCase
              //I want how to replace the charater at postion 0 in numaric with result
            }
Similar Threads
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Aug 2nd, 2009
0

Re: Replace character at a particular position

Hi sonia,

you could try this:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function ConvertToUpperCase() {
  2. var result;
  3. var numaric = "sonia";
  4. var Character = numaric.charAt(0);
  5. result = numaric.replace( Character, Character.toUpperCase() ); // Converting target character to uppercase letter.
  6. }
Last edited by essential; Aug 2nd, 2009 at 2:18 pm.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 2nd, 2009
0

Re: Replace character at a particular position

I would try something like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function ConvertToUpperCase()
  2. {
  3. var result;
  4. var numaric = "sonia";
  5. result=numaric.charAt(0).toUpperCase() + numaric.substring(1);
  6. }

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

HTH

Stoo

--
<FAKE SIGNATURE>
Last edited by peter_budo; Aug 4th, 2009 at 5:57 am. Reason: Keep It On The Site - Do not manually post "fake" signatures in your posts. Instead, you may create a sitewide signature within the user control panel.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
we evolve is offline Offline
2 posts
since Aug 2009
Aug 2nd, 2009
0

Re: Replace character at a particular position

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
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 2nd, 2009
0

Re: Replace character at a particular position

THX BOTH OF U
Working Code
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. if (!IsPostBack)
  6. {
  7. txtCompanyName.Attributes.Add("onblur", "javascript : ConvertToUpperCase()");
  8.  
  9. }
  10. }
  11. catch (Exception ex)
  12. {
  13. lblStatus .Text =ex.Message .ToString ();
  14. }
  15. }
  16.  
  17.  
  18. function ConvertToUpperCase()
  19. {
  20. var numaric = document.getElementById ("txtCompanyName").value;
  21. numaric =numaric.charAt(0).toUpperCase() + numaric.substring(1);
  22. var pos = numaric.indexOf(" ");
  23. while(pos > -1)
  24. {
  25. numaric =numaric.substring(0,pos+1) + numaric.charAt(pos+1).toUpperCase() + numaric.substring(pos+2);
  26. pos = numaric.indexOf(" ", pos+1);
  27. }
  28. document.getElementById ("txtCompanyName").value=numaric;
  29. }


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?
Last edited by sonia sardana; Aug 2nd, 2009 at 4:11 pm.
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Aug 2nd, 2009
0

Re: Replace character at a particular position

Click to Expand / Collapse  Quote originally posted by essential ...
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>
Last edited by peter_budo; Aug 4th, 2009 at 5:57 am. Reason: Keep It On The Site - Do not manually post "fake" signatures in your posts. Instead, you may create a sitewide signature within the user control panel.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
we evolve is offline Offline
2 posts
since Aug 2009
Aug 2nd, 2009
1

Re: Replace character at a particular position

Sonia,

You might like to try something like this:
javascript Syntax (Toggle Plain Text)
  1. //This is a general utility function that returns an Initial Caps version of the input string
  2. function initialCaps(str){
  3. var a = str.split(' ');
  4. for(var i=0; i<a.length; i++){
  5. ch = a[i].charAt(0);
  6. a[i] = a[i].replace(ch, ch.toUpperCase());
  7. }
  8. return a.join(' ');
  9. }
  10.  
  11. //This function is applies initialCaps() to a specified form field.
  12. function field2InitialCaps(filedId){
  13. var f = document.getElementById (filedId);
  14. if(f) { f.value = initialCaps(f.value); }
  15. }
html Syntax (Toggle Plain Text)
  1. <!-- Demo -->
  2. <input id="txtCompanyName" type="text" value="sonia is a beautiful name" size="50" /><br />
  3. <a href="" onclick="field2InitialCaps('txtCompanyName');return false">Convert to Initial Caps</a>
Airshow
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: inserting img into table
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: generating a table structure using JQuery





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC