java 怎么取cccc-sss中“-“前的字符串?

可以使用 String 的 split 方法来分割字符串,然后取出第一个元素,例如:

String str = "cccc-sss";
String[] parts = str.split("-");
String result = parts[0];
System.out.println(result); // 输出 "cccc"

另外,如果只需要取出第一个"-"前的字符串,也可以使用 String 的 substring 方法,例如:

String str = "cccc-sss";
int index = str.indexOf("-");
String result = str.substring(0, index);
System.out.println(result); // 输出 "cccc"

猜你喜欢

转载自blog.csdn.net/weixin_36557877/article/details/130286874