I'm trying to split the string with double pipe(||) being the delimiter.String looks something like this:
String str ="[email protected]||[email protected]||[email protected]";
i'm able to split it using the StringTokeniser.The javadoc says the use of this class is discouraged and instead look at String.split as option.
StringTokenizer token = new StringTokenizer(str, "||");
The above code works fine.But not able to figure out why below string.split function not giving me expected result..
String[] strArry = str.split("\\||");
Where am i going wrong..?
This technique is used when there is a multiple character through which we would like to split the string. For example, in a message log, let us say a particular string is occurring after every sentence instead of a full stop.
The split method returns the new array. Also, when the string is empty, split returns an array containing one empty string, rather than an empty array. On compiling, it will generate the same code in JavaScript.
There are many ways to split strings by a specific separator function in Go. Below are some of the easy ways of doing string splitting in GoLang. 1. Split String using the split() function . The strings package contains a function called split(), which can be used to split string efficiently. The method usage is shown below.
Now one thing to watch out for is the location of split of a string. This might be a single character or even combination of multiple characters. The location or the pattern on which it is decided to split the string is known as delimiter.
String.split()
uses regular expressions. You need to escape the string that you want to use as divider.
Pattern has a method to do this for you, namely Pattern.quote(String s)
.
String[] split = str.split(Pattern.quote("||"));
You must escape every single |
like this str.split("\\|\\|")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With