0

I have a String like this "+91 2895675148 / +91 123456789 / +91 987654321". I want to split above string into

 String str1 = +91 2895675148

 String str2 = +91 1234567

 String str3 = +91 987654321

How to make separate the above numbers from string in java without using index as a parameter

Thanks

Shiv
  • 43
  • 9

2 Answers2

1
String string = "+91 2895675148 / +91 123456789 / +91 987654321";
String[] sections = string.split(" / ");
String part1 = parts[0]; // +91 2895675148
String part2 = parts[1]; // +91 123456789
String part3 = parts[2]; // +91 987654321

Please view for more methods: link

Community
  • 1
  • 1
Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
  • I want to replace \n character instead of '/' this. How to do that . Is I have to follow the same method?? – Shiv Dec 11 '15 at 10:45
  • If I understood you correctly you want to replace / with \n in the String ? In that case use String[] sections = string.split("\\\n\\]"); Since \n is a special newline character you have to escape it. – Afshin Ghazi Dec 11 '15 at 10:51
  • how to get each number into separate string? I mean str1 = +91 2895675148, str2 = +91 123456789 etc – Shiv Dec 11 '15 at 11:14
  • String part1 = parts[0]; // +91 2895675148 String part2 = parts[1]; // +91 123456789 String part3 = parts[2]; // +91 987654321 – Afshin Ghazi Dec 11 '15 at 12:08
1

Your can use split("/") which returns array of string.

Eg.

String str = "+91 2895675148 / +91 123456789 / +91 987654321";
System.out.println(Arrays.toString(str.split("/"))); 

out put :-

[+91 2895675148 ,  +91 123456789 ,  +91 987654321]
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
  • how to get each number into separate string? I mean str1 = +91 2895675148, str2 = +91 123456789 etc. – Shiv Dec 11 '15 at 11:14