2017-09-04 1 views
0

j'ai une chaîne avec des signes et je veux obtenir les signes seulement et les mettre dans un tableau de chaînes, voici ce que je l'ai fait:signes Extraction/symboles d'une chaîne en 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(" "); 

Cependant le 2ème élément du signeArray est un espace, [+,, -, +]

Merci pour votre temps.

+0

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

Répondre

2

Vous pouvez le faire de plusieurs façons. Soit remplacer plusieurs chiffres adjacents avec un seul espace:

// replace the numbers with spaces, leaving the signs and spaces 
String signString = str.replaceAll("[0-9]+", " "); 

Ou bien dans la dernière étape, divisée sur plusieurs espaces:

// then get an array that contains the signs without spaces 
String[] signsArray = signString.trim().split(" +"); 
+0

qui le fait, merci! –

1

Il suffit de remplacer " " à "" dans votre code

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

Cela devrait fonctionner pour vous. Vive

Image Code fonctionnement enter image description here

+0

Ce n'est pas le cas, mais en utilisant: str.replaceAll ("[^ + -] +", ""); ou str.replaceAll ("[0-9] +", ""); fait, merci pour votre aide :) –

+0

cela a fonctionné pour moi, j'ai joint l'image aussi ..toutes votre problème résolu – iamdeowanshi