在Java中从字符串中提取符号
我有一个带符号的字符串,我只想获取符号并将它们放入字符串数组中,下面是我所做的:在Java中从字符串中提取符号,java,arrays,string,Java,Arrays,String,我有一个带符号的字符串,我只想获取符号并将它们放入字符串数组中,下面是我所做的: String str = "155+40-5+6"; // replace the numbers with spaces, leaving the signs and spaces String signString = str.replaceAll("[0-9]", " "); // then get an array that contains the signs without spaces Strin
String str = "155+40-5+6";
// replace the numbers with spaces, leaving the signs and spaces
String signString = str.replaceAll("[0-9]", " ");
// then get an array that contains the signs without spaces
String[] signsArray = stringSigns.trim().split(" ");
然而,signsArray的第二个元素是一个空格,[+,-,+]
谢谢您的时间。您可以用几种方法来完成。用单个空格替换多个相邻数字:
// replace the numbers with spaces, leaving the signs and spaces
String signString = str.replaceAll("[0-9]+", " ");
或者,在最后一步中,在多个空间上拆分:
// then get an array that contains the signs without spaces
String[] signsArray = signString.trim().split(" +");
你可以用几种方法来做。用单个空格替换多个相邻数字:
// replace the numbers with spaces, leaving the signs and spaces
String signString = str.replaceAll("[0-9]+", " ");
或者,在最后一步中,在多个空间上拆分:
// then get an array that contains the signs without spaces
String[] signsArray = signString.trim().split(" +");
只需将代码中的“
替换为”
String str = "155+40-5+6";
// replace the numbers with spaces, leaving the signs and spaces
String signString = str.replaceAll("[0-9]","");
// then get an array that contains the signs without spaces
String[] signsArray = stringSigns.split("");
这应该对你有用。干杯
运行代码的映像
只需将代码中的“
替换为”
String str = "155+40-5+6";
// replace the numbers with spaces, leaving the signs and spaces
String signString = str.replaceAll("[0-9]","");
// then get an array that contains the signs without spaces
String[] signsArray = stringSigns.split("");
这应该对你有用。干杯
运行代码的映像
它没有,但使用:str.replaceAll(“[^+-]+”,”);或str.replaceAll(“[0-9]+”,”);是的,谢谢你的帮助:)它对我有效,我还附上了图片..无论如何你的问题解决方案没有,但使用:str.replaceAll(“[^+-]+”,”);或str.replaceAll(“[0-9]+”,”);是的,谢谢你的帮助:)这对我很有效,我还附上了图片。不管怎样,你的问题解决了