String Tokenizer or Split function [closed]
I have a String "|Republic of Ireland| v/s |Northern Ireland|". This is not a fixed String. In place of "v/s" it can be any other word. I want only "Replublic of Ireland" and "Northern Ireland" in my output. Please tell me how to do it by using String Tokenizer or Split function. I want this in Java language.
You can use the split
function as:
String str = "|Republic of Ireland| v/s |Northern Ireland|";
String[] pieces = str.replaceAll("^\\||\\|$","").split("\\|[^\\|]+\\|");
See it
Unless you are planing on always having to parse v/s
just use string replace to get rid of |, and then use a split on v.s
Python:
"|Republic of Ireland| v/s |Northern Ireland|".split("v/s")[0].replace("|", "")
Java
"|Republic of Ireland| v/s |Northern Ireland|".replace('|', '').split("v\s")[0];
As you can see its pretty much the same in all languages.
If you have alternating items of interest in your string, split it at the pipe character and use every second item in your resulting array.
Please specify your programming language to obtain a more detailed result.
精彩评论