How to get the second word of a string in Java

1 Answer

0 votes
public class GetTheSecondWordOfString_Java {
    public static void main(String[] args) {
        String str = "c java c# python c++ rust";
        String[] words = str.split(" ");
        
        if (words.length >= 2) {
            System.out.println("The second word is: " + words[1]);
        } else {
            System.out.println("The sentence does not have a second word.");
        }
    }
}


/*
run:

The second word is: java

*/

 



answered Oct 3, 2024 by avibootz

Related questions

1 answer 231 views
1 answer 216 views
1 answer 229 views
2 answers 318 views
...