gpt4 book ai didi

java - 我无法让 String split 方法正常工作

转载 作者:行者123 更新时间:2023-11-30 06:40:40 26 4
gpt4 key购买 nike

我试图在“^”前后将数组中的一个字符串拆分为多个字符串。我使用了 String split 方法,但它只是将整个 String 存储在数组的第一个值中。

输出:b[0]= x^2预期:b[0] = x , b[1] = 2

代码如下:

public class test {
public static void main(String[] args) {
String a[] = {"x^2"};
String b[] = a[0].split("^");

System.out.println(b[0]);
}
}

最佳答案

Caret ^正则表达式中的一个特殊字符,表示String的开头,用\转义>。喜欢,

String a[] = {"x^2"};
String b[] = a[0].split("\\^");

System.out.println(b[0]);

关于java - 我无法让 String split 方法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com