简体   繁体   English

Java String.split,具有不同的字符格式

[英]Java String.split with varied character format

I have strings of the following format 我有以下格式的字符串

(1, 3, value)

I want to split this string using String.split so that I can get the three values without the delemeters. 我想使用String.split拆分此字符串,以便我可以获得没有delemeter的三个值。 I want the output to look like 我希望输出看起来像

1
3
value

The problem is that the value sometimes contains delimeter values itself, such as the string revolution_(1848) 问题是该值有时包含分隔符值本身,例如字符串revolution_(1848)

How can I do this with String.split(), so that I can split the words based on commas and inside the brackets, so I get only the three values. 我怎么能用String.split()来做这个,这样我就可以根据逗号和括号内部分割单词,所以我只得到三个值。

Thanks. 谢谢。

Assuming the parentheses are always on the outside, just don't consider them and split on the , s: 假设括号总是在外面,只是不要考虑它们并拆分, s:

String toSplit = "(1, 3, value, revolution_(1848))";
toSplit = toSplit.substring(1,toSplit.length() - 1); //ignore wrapping characters.
String[] splitted = toSplit.split(",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM