0

In Java, how would I get a substring of a certain character followed by a number?

The string looks like this:

To be, or not to be. (That is the question.) (243)

I want the substring up until the (243), where the number inside the parenthesis is always changing every time I call.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
djangofan
  • 28,471
  • 61
  • 196
  • 289

6 Answers6

2

Use a regular expression:

newstr = str.replaceFirst("\(\d+\)", "");

What this means is to find a substring beginning with (, then any number of digits, and then the character ). Then replace the substring with the empty string, "".

Reference: java.lang.String.replaceFirst()

i-bob
  • 423
  • 2
  • 5
  • I didn't realize that the String class had built-in methods (in this case called replaceFirst) that recognized a regular expressions. This is the answer I was really looking for. – djangofan Jan 20 '14 at 20:49
  • I think you might want to anchor your regular expression to the end of the line using $ in case there happens to be a parenthesized number in your string, like `There are four (4) lights! (345)` – PurpleVermont Jan 21 '14 at 03:02
0

You could match it with a regex, and get the index of the regex. Then use that to get the index in the string.

An example of that is Can Java String.indexOf() handle a regular expression as a parameter?

Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
if(matcher.find()){
    System.out.println(matcher.start());//this will give you index
}
Community
  • 1
  • 1
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
0

You can use String.replaceAll():

String s = "To be, or not to be. (That is the question.) (243)";
String newString = s.replaceAll("\\(\\d+\\).*", "");
Keppil
  • 45,603
  • 8
  • 97
  • 119
0

I think you can actually just do something like:

mystring.substring(0,mystring.lastIndexOf"("))

assuming that the last thing on the line will be the number in parentheses.

PurpleVermont
  • 1,179
  • 4
  • 18
  • 46
  • I'm not sure if you need to escape the `(` in quotes there or not -- you could also specify it as a `char` rather than a string, which may be more efficient too. – PurpleVermont Jan 20 '14 at 20:19
0

You could use a for loop and add the characters before the number to a separate string

String sentence = "To be, or not to be. (That is the question.) (243)";

public static void main(String[] args) {
    String subSentence = getSubsentence(sentence);
}

public String getSubsentence(String sentence) {
    String subSentence = "";
    boolean checkForNum = false;
    for (int i = 0; i < sentence.length(); i++) {
        if (checkForNum) {
            if (isInteger(sentence.getSubstring(i, i+1))) return subSentence;
            checkForNum = false;
        } else {
            if (sentence.getSubstring(i, i+1).equals("(")) checkForNum = true;
            else subSentence += sentence.getSubstring(i, i+1);
        }
    }
    return subSentence;
}

public boolean isInteger(String s) {
try { 
    Integer.parseInt(s); 
} catch(NumberFormatException e) { 
    return false; 
}
    return true;
}
Tyler
  • 486
  • 1
  • 6
  • 20
0

Using a regex this can be solved with.

public class RegExParser {

    public String getTextPart(String s) {

        String pattern = "^(\\D+)(\\s\\(\\d+\\))$";

        String part = s.replaceAll(pattern, "$1");
        return part;

    }
}

Simple and performance is good.

DukeMe
  • 167
  • 2
  • 8