remove underscore from string
I need to remove a underscore(_) from a string, I am using the replace function bt it does not get removed? Does anybody know why.
here is the code
Code:
ISSUE_support = "Joe_Smith";
ISSUE_support.replace("_"," ");
Code:
ISSUE_support = "Joe_Smith";
ISSUE_support = ISSUE_support.replace("_"," ");
Last edited by letmehaveago; 07-13-2010 at 08:31 AM .
Reason: forgot something
1. replace() is a method. You need to give your variable the new value with an assignment :
Code:
ISSUE_support = "Joe_Smith";
ISSUE_support=ISSUE_support.replace("_"," ");
2. On the other hand are you aware that the method, as you have written, will replace only the first occurrence of the underscore in the string? If you need multiple replacements, you must use a Regular Expression:
Code:
ISSUE_support=ISSUE_support.replace(/_/g ," ");
Last edited by Kor; 07-13-2010 at 08:35 AM .
Thanks for the second point.
Originally Posted by
letmehaveago
Thanks for the second point.
The moment I have learned them, Regular Expressions changed my life
http://lawrence.ecorp.net/inet/samples/regexp-intro.php
I have been playing around with regular expressions for sometime too. I use it with vim.
What I am concerned is http://www.w3schools.com/jsref/jsref_replace.asp doesn't even document that. I probably should default to https://developer.mozilla.org/en/Cor...String/replace but no browser compatibility list.
Originally Posted by
letmehaveago
Oh, but it does, See Example 3 : "Perform a global, case-insensitive search [...] "
Oh, but it does, See Example 3 : "Perform a global, case-insensitive search [...] "
Yes, but would have been more useful in the description or definition. It is a bit unintuitive for me too - php and java both replace all occurrences.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks