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.

5
  • 4
    Show some sample input and output, and the rule for determining how much of the "end of string" you want to extract. Commented Aug 8, 2011 at 17:21
  • age: old I want to extract old from the end. Commented Aug 8, 2011 at 17:23
  • 1
    @Ken: What do you mean by "extract"? Commented Aug 8, 2011 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? Commented Aug 8, 2011 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. Commented Aug 8, 2011 at 17:27

6 Answers 6

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This is what I wanted. I'm just bad at explaining things.
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

Comments

0

You mean like:

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

Comments

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...
    }
}

Comments

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.

Comments

0

You could use this Java code to solve your problem:

String suffix = "old";

if(str.endsWith(suffix)) {
    System.out.println(suffix);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.