0

So in Java, I know that str.endsWith(suffix) tests if str ends with something. Let's say I have a text with the line "You are old" in it. How would I take the "old" and set it as a variable so I can print it out in the console?

I know I could do:

if(str.endsWith("old")){ 
   String age = "old";
}

But then I'm going to have more options, so then I'd have to do:

if(str.endsWith("option1")){ 
   String age = "option1";
}

if(str.endsWith("option2")){ 
   String age = "option2";
}

...

Is there a more efficient and less verbose way to check the end of strings over writing many, possibly hundreds, of if statements

Format:
    setting: option
    setting2: option2
    setting3: option3 ...

Regardless of what "option" is, I want to set it to a variable.

  • 4
    Show some sample input and output, and the rule for determining how much of the "end of string" you want to extract. – Jim Garrison Aug 08 '11 at 17:21
  • `age: old` I want to extract old from the end. –  Aug 08 '11 at 17:23
  • 1
    @Ken: What do you mean by "extract"? – tskuzzy Aug 08 '11 at 17:24
  • 1
    Show some examples. Is it always the string "old"? Is it the stuff after the `:` colon character? What are the rules? – Jim Garrison Aug 08 '11 at 17:24
  • 1
    If you're searching for "old", then what are you extracting? "old" is "old". If you want stuff *around* "old", that's a different question, but not one you've currently asked. For example, if you have strings like "My age is [someString]", and you have different possibilities for [someString], then you could use a regular expression to find that. What you do with the strings you find will still require an if/switch case, but at least the parsing will be in one statement. – dlev Aug 08 '11 at 17:27

6 Answers6

2

If you are working with sentences and you want to get the word, do

String word = str.substring(str.lastIndexOf(" "));

You may need a +1 after the lastIndexOf() to leave the space out. Is that what you are looking for?

yoozer8
  • 7,361
  • 7
  • 58
  • 93
1

Open your file and read the line with the readLine() method. Then to get the last word of the string you can do as it is suggested here

Community
  • 1
  • 1
talnicolas
  • 13,885
  • 7
  • 36
  • 56
0

You could use this Java code to solve your problem:

String suffix = "old";

if(str.endsWith(suffix)) {
    System.out.println(suffix);
}
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
0

You mean like:

String phrase = "old";
if(str.endsWith(old)){ 
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

Is this what you're looking for?

List<String> suffixes = new ArrayList<String>();
suffixes.add("old");
suffixes.add("young");

for(String s: suffixes)
{
    if (str.endsWith(s))
    {
        String age = s;

        // .... more of your code here...
    }
}
Kurru
  • 14,180
  • 18
  • 64
  • 84
0

If you're worried about repeating very similar code, the answer is always (99%) to create a function,

So in your case, you could do the following:

public void myNewFunction(String this, String that){
    if(this.endsWith(that)){ 
    String this = that;

    }
}

...

String str = "age: old";
myNewFunction(str, "old"); //Will change str
myNewFunction(str, "new"); //Will NOT change str

And if that is too much, you can create a class which will do all of this for you. Inside the class, you can keep track of a list of keywords. Then, create a method which will compare a given word with each keyword. That way, you can call the same function on a number of strings, with no additional parameters.

Peaches491
  • 1,239
  • 15
  • 27