Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove prefix from a list of strings

How can I remove a sub-string (a prefix) from a array of string elements? (remove the sub string from each element)

like image 465
Alex Avatar asked Nov 14 '11 22:11

Alex


People also ask

How do I remove a prefix from a list in Python?

The most commonly known methods are strip() , lstrip() , and rstrip() . Since Python version 3.9, two highly anticipated methods were introduced to remove the prefix or suffix of a string: removeprefix() and removesuffix() .

What is remove prefix?

removeprefix(prefix, /) function which removes the prefix and returns the rest of the string. If the prefix string is not found then it returns the original string. It is introduced in Python 3.9. 0 version. Syntax: str.removeprefix(prefix, /)

How do I remove the beginning of a string in Python?

Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.


1 Answers

Using RegExp and ^ to ensure it is the prefix and not just somewhere in the string:

var arr = ['a1', 'a2', 'a54a']; for(var i = 0, len = arr.length; i < len; i++) {     arr[i] = arr[i].replace(/^a/, ''); } arr; // '1,2,54a' removing the 'a' at the begining 
like image 99
Joe Avatar answered Sep 22 '22 09:09

Joe