[英]How to split a string like p^q using split function in Java?
I want to split a string like "p^q" into two strings, ie "p" and "q". 我想将“ p ^ q”之类的字符串拆分为两个字符串,即“ p”和“ q”。 How can I do this using "string.split" in Java.
如何使用Java中的“ string.split”执行此操作。 I have used syntaxes like
s.split("^")
, s.split("\\^")
, and s.split("\\\\^")
! 我使用了
s.split("^")
, s.split("\\^")
和s.split("\\\\^")
! But the arraylist always has one element! 但是arraylist总是只有一个元素! Thanks
谢谢
System.out.println(Arrays.toString("p^q".split("\\^")));
prints 版画
[p, q]
which proves that the array returned by split
has not one, but two elements, and exactly those that you require. 这证明
split
所返回的数组不是只有一个,而是只有两个元素,而正是您需要的那些元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.