-1

This is my string address : London, Jon 2 A

And I want to see in my output see London, Jon

I tried to do this :

String result = chapterNumber.substring(0, chapterNumber.indexOf("1"));

But I have to do 10 times from different number maybe is better way to do this

bhavna garg
  • 270
  • 2
  • 19
Krzysztof Pokrywka
  • 1,356
  • 4
  • 27
  • 50

4 Answers4

8

try this

     String str = "London, Jon 2 A";
     System.out.println(str.replaceAll("\\d.*",""));
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

You can use regex with groups to get only the first group (the sequence of non-digit chars until the first digit):

String result = chapterNumber.replaceAll("([^\\d])(\\d.*)", "$1");
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
0
String s = "London, Jon 2 A";
Matcher matcher = Pattern.compile("\\d+").matcher(s);
matcher.find();
int i = Integer.valueOf(matcher.group());

System.out.println(s.substring(0,s.indexOf(String.valueOf(i))));
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Frank
  • 873
  • 1
  • 7
  • 17
0
String result = chapterNumber.substring(0, chapterNumber.indexOf(","));
bhavna garg
  • 270
  • 2
  • 19