Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove text from a string?

I've got a data-123 string.

How can I remove data- from the string while leaving the 123?

like image 764
Michael Grigsby Avatar asked May 01 '12 14:05

Michael Grigsby


People also ask

How do you remove text from a string in Python?

You can remove a character from a Python string using replace() or translate(). Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string without a replacement.

How do I remove a specific character from a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


1 Answers

var ret = "data-123".replace('data-','');  console.log(ret);   //prints: 123

Docs.


For all occurrences to be discarded use:

var ret = "data-123".replace(/data-/g,''); 

PS: The replace function returns a new string and leaves the original string unchanged, so use the function return value after the replace() call.

like image 160
Mathletics Avatar answered Oct 01 '22 16:10

Mathletics