开发者

String Tokenizer or Split function [closed]

开发者 https://www.devze.com 2023-03-09 17:40 出处:网络
开发者_如何学Go It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
开发者_如何学Go It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消