trim a string help

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2008
Posts: 74
Reputation: sacarias40 is an unknown quantity at this point 
Solved Threads: 0
sacarias40's Avatar
sacarias40 sacarias40 is offline Offline
Junior Poster in Training

trim a string help

 
0
  #1
May 25th, 2009
i have a string like this:


JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. location = '#http://www.website.com';

how can i take off that hash sine?
regards
no dejes para mañana lo que puedes hacer hoy
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: trim a string help

 
0
  #2
May 25th, 2009
You can use the replace() method.
e.g.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. loc = "#http://www.wesite.com/".replace(/#/ig, "");
  2. alert( loc );
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 74
Reputation: sacarias40 is an unknown quantity at this point 
Solved Threads: 0
sacarias40's Avatar
sacarias40 sacarias40 is offline Offline
Junior Poster in Training

Re: trim a string help

 
0
  #3
May 25th, 2009
thank you
no dejes para mañana lo que puedes hacer hoy
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Re: trim a string help

 
0
  #4
May 25th, 2009
Originally Posted by essential View Post
You can use the replace() method.
e.g.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. loc = "#http://www.wesite.com/".replace(/#/ig, "");
  2. alert( loc );
You don't want to do a global search on this one, just remove the hash at the beginning. Otherwise, the URL you want to redirect to may have an anchor on it, e.g. #http://www.website.com/test.html#Contacts .

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. loc = "#http://www.wesite.com/".replace(/^#*/i, "");
  2. alert(loc);
Last edited by itsjareds; May 25th, 2009 at 3:30 am.
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: trim a string help

 
0
  #5
May 25th, 2009
No it's not that bad.
If you will apply proper techniques and good workaround in your script, im sure everything can be organized.

Here's a short example, so that you can see the big difference between the codes.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  7. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  9. <title>CROSS-PLATFORM</title>
  10. <script type="text/javascript">
  11. // <![CDATA[
  12. var include, url;
  13.  
  14. include = function( url ) {
  15. loc = "#http://www.web#site.#com/#contact".replace(/^#*/i, "");
  16.  
  17. alert( loc ); // Check the first pattern and observed the output w/o g-flag.
  18.  
  19. delete loc;
  20.  
  21. loc = "#http://www.web#site.#com/".replace(/#/ig, ""); // With g-flag's!
  22.  
  23. alert( loc + url ) // Now we will use it with a global flag and by adding up some arguments' we can organized everything and also remove unnecessary characters in the url.
  24. }
  25.  
  26. window.onload = include( "#contact" );
  27.  
  28. // ]]>
  29. </script>
  30. </head>
  31. <body>
  32.  
  33. </body>
  34. </html>
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Re: trim a string help

 
0
  #6
May 25th, 2009
I suppose it depends on what your circumstances are. I assumed that he was using AJAX and had a link like what he proposed. If he just needed to remove the first #, he could also have done:

  1. var string = "#http://www.example.com";
  2. var newString = string.substr(1);
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: trim a string help

 
0
  #7
May 25th, 2009
Hi itsjared,

It's was just a logical example of using g-flag's.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC