xxxxxxxxxx
String.prototype.splitAndKeep = function(separator, method='seperate'){
var str = this;
if(method == 'seperate'){
str = str.split(new RegExp(`(${separator})`, 'g'));
}else if(method == 'infront'){
str = str.split(new RegExp(`(?=${separator})`, 'g'));
}else if(method == 'behind'){
str = str.split(new RegExp(`(.*?${separator})`, 'g'));
str = str.filter(function(el){return el !== "";});
}
return str;
};
xxxxxxxxxx
# Split the string using the separator
text= "Orange,Apple,Grapes,WaterMelon,Kiwi"
print(text.split(','))
xxxxxxxxxx
# split strings around given separator/delimiter
# import Pandas as pd
import pandas as pd
# create a new data frame
df = pd.DataFrame({'Location': ['Cupertino,California', 'Los Angles, California', 'Palo Alto, California']
})
df[['City','State']] = df.Location.str.split(',',expand=True)
df
print(df)
xxxxxxxxxx
def split_words_separators(an_input: str) -> list:
"""Returns two lists, one with words, and one with the separators used in input string"""
merged = re.split(r"([ ,]+)", an_input)
# the [::2] is to get the even indexes, which are the words
# the [1::2] is to get the odd indexes, which are the separators
return [merged[::2], merged[1::2]]