Replace character at a particular position

Please support our JavaScript / DHTML / AJAX advertiser: $6.99 Domain Names at 1&1. Includes Free Privacy. Save Now!
Thread Solved

Join Date: Mar 2008
Posts: 327
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Replace character at a particular position

 
0
  #1
Aug 2nd, 2009
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
            }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 961
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 135
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Replace character at a particular position

 
0
  #2
Aug 2nd, 2009
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 1:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: we evolve is an unknown quantity at this point 
Solved Threads: 1
we evolve we evolve is offline Offline
Newbie Poster

Re: Replace character at a particular position

 
0
  #3
Aug 2nd, 2009
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 4: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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 961
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 135
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Replace character at a particular position

 
0
  #4
Aug 2nd, 2009
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
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 327
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Replace character at a particular position

 
0
  #5
Aug 2nd, 2009
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 3:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: we evolve is an unknown quantity at this point 
Solved Threads: 1
we evolve we evolve is offline Offline
Newbie Poster

Re: Replace character at a particular position

 
0
  #6
Aug 2nd, 2009
Originally Posted by essential View Post
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 4: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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 971
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 144
Airshow's Avatar
Airshow Airshow is offline Offline
Posting Shark

Re: Replace character at a particular position

 
1
  #7
Aug 2nd, 2009
Sonia,

You might like to try something like this:
  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. }
  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
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1434 | Replies: 6
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC