Java selecting words from a string

后端 未结 3 1437
余生分开走
余生分开走 2020-12-19 16:01

HI Everyone. I\'m sorry for this embarrassingly newbie question but I cannot seem to figure out the command to do it. I\'m okay with python and had a script in jython that I

3条回答
  •  温柔的废话
    2020-12-19 16:17

    I think you are looking for String.split.

    String s = "Java is really cool";
    String words[] = s.split(" ");
    String firstTwo = words[0] + "  " + words[1]; // first two words
    String lastTwo = words[words.length - 2] + " "
            + words[words.length - 1]; // last two words
    

提交回复
热议问题