如何在java中的String的split()中使用模式
我输入了格式,
"MG3 4F W 123 TO GH2 2F E 345"
or
"MG3 4F W 123 To GH2 2F E 345"
or
"MG3 4F W 123 tO GH2 2F E 345"
or
"MG3 4F W 123 to GH2 2F E 345"
我必须根据“TO”分割这种字符串。下面是我尝试使用模式分割它的代码,但它给出了编译错误,你能帮我吗???
public static final String DIVIDER_PATTERN = "{tT}{oO}";
public static void main(String[] args)
{
Pattern pt = Pattern.compile(DIVIDER_PATTERN);
String hello = "Hello to World TO this tO test To how";
String array[] = hello.split(DIVIDER_PATTERN);
System.out.println(array[0]);
}
I have input of the format
"MG3 4F W 123 TO GH2 2F E 345"
or
"MG3 4F W 123 To GH2 2F E 345"
or
"MG3 4F W 123 tO GH2 2F E 345"
or
"MG3 4F W 123 to GH2 2F E 345"
i have to split this kind of string based on "TO".Below is the code which i tried to used to split it using pattern but it gives compilation error can you please help me ???
public static final String DIVIDER_PATTERN = "{tT}{oO}";
public static void main(String[] args)
{
Pattern pt = Pattern.compile(DIVIDER_PATTERN);
String hello = "Hello to World TO this tO test To how";
String array[] = hello.split(DIVIDER_PATTERN);
System.out.println(array[0]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
{tT}{oO}
更改为[tT][oO]
。方括号表示 字符类< /a>,而大括号则完全用于其他用途(重复运算符)。
Change
{tT}{oO}
to[tT][oO]
.It is square brackets that denote character classes, whereas curly braces are used for something else entirely (the repetition operator).