50

I have a string |0|0|0|0

but it needs to be 0|0|0|0

How do I replace the first character ('|') with (''). eg replace('|','')

(with JavaScript)

1
  • This is a common misconception coming from other languages, in JavaScript the .replace() method doesn't replace all occurrences (unless you use /g), it only replaces the first occurence: w3schools.com/jsref/jsref_replace.asp Commented Jun 7, 2010 at 19:09

8 Answers 8

102

You can do exactly what you have :)

var string = "|0|0|0|0";
var newString = string.replace('|','');
alert(newString); // 0|0|0|0

You can see it working here, .replace() in javascript only replaces the first occurrence by default (without /g), so this works to your advantage :)

If you need to check if the first character is a pipe:

var string = "|0|0|0|0";
var newString = string.indexOf('|') == 0 ? string.substring(1) : string;
alert(newString); // 0|0|0|0​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

You can see the result here

Sign up to request clarification or add additional context in comments.

2 Comments

@MgS: Test it - .replace() only replaces the first occurence, do you want to remove the | only if it's leading? I updated the answer to reflect this option as well :)
@Justin - thanks! it was a mis-paste, fixed it in the answer.
32
str.replace(/^\|/, "");

This will remove the first character if it's a |.

Comments

26
var newstring = oldstring.substring(1);

3 Comments

If the string is 0|0|0|0 then it will remove the first 0
@MgS, true. So check first to see what the first character is.
@Mgs The title of the question asks how to remove the first character, not remove the first instance of a specific character. This is the general solution.
3

If you're not sure what the first character will be ( 0 or | ) then the following makes sense:

// CASE 1:
var str = '|0|0|0';
str.indexOf( '|' ) == 0 ? str = str.replace( '|', '' ) : str;
// str == '0|0|0'

// CASE 2:
var str = '0|0|0';
str.indexOf( '|' ) == 0? str = str.replace( '|', '' ) : str;
// str == '0|0|0'

Without the conditional check, str.replace will still remove the first occurrence of '|' even if it is not the first character in the string. This will give you undesired results in the case of CASE 2 ( str will be '00|0' ).

Comments

3

Try this:

var str = "|0|0|0|0";
str.replace(str.charAt(0), "");

Avoid using substr() as it's considered deprecated.

Comments

1

It literally is what you suggested.

"|0|0|0".replace('|', '')

returns "0|0|0"

1 Comment

If string is 0|0|0 then it will remove the first 0
0

We don't want to delete the next pipe in case if string doesn't start with it. Then Mathew Flaschen's regex approach is a good solution. Without regex;

  str = str[0] == "/"? str.replace("/", ""):str;

Comments

-1
"|0|0|0|0".split("").reverse().join("")  //can also reverse the string => 0|0|0|0|

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.