Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to remove special char in string array?

Tags:

java

arrays

I'm having an array of strings (phone numbers) and i need to remove +, which is in front of one of the elements (numbers) since it throws an NumberFormatException when i try to cast it to int.

The elements in array are 0888888888 0888123456 +359886001122 and i have already used .split(" ") in order to separate them. I've also tried .split(" +") in order to remove + from the last one, but this didn't work.

like image 556
Иван Тонов Avatar asked Sep 01 '26 11:09

Иван Тонов


1 Answers

You have to use replaceAll instead of split, for example :

"0888888888 0888123456 +359886001122".replaceAll("\\+", "");

this will show you :

0888888888 0888123456 359886001122
//-------------------^------------

Then if you want to split each number you can use split(" ") like this :

String numbers[] = "0888888888 0888123456 +359886001122".replaceAll("\\+", "").split(" ");
System.out.println(Arrays.toString(numbers));

this will give you :

[0888888888, 0888123456, 359886001122]

EDIT

Or like @shmosel said in comment you ca use replace("+", "")

like image 139
YCF_L Avatar answered Sep 04 '26 02:09

YCF_L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!