2017-09-04 137 views
0

字符串我有跡象字符串,我想只得到標誌,並把它們在一個字符串數組,這裏是我做了什麼:提取標誌/符號從Java中

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(" "); 

然而signArray的第二個元素是一個空格,[+, - - ,+]

謝謝你的時間。

+0

https://stackoverflow.com/questions/17516049/java-removing-numeric-values-from-string – soorapadman

回答

2

你可以通過幾種方法做到這一點。無論是用一個空格代替多個相鄰數字:

// 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(" +"); 
+0

這就行了,謝謝! –

1

只需更換" """在你的代碼

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(""); 

這應該適合你。乾杯

的運行代碼圖像 enter image description here

+0

它不會,但使用:str.replaceAll(「[^ + - ] +」,「」);或str.replaceAll(「[0-9] +」,「」);沒有,謝謝你的幫助:) –

+0

它爲我工作,我有附加圖像也......通常你的問題解決了 – iamdeowanshi