Find the words start from a special character java

后端 未结 5 1654
梦毁少年i
梦毁少年i 2021-02-08 21:18

I want to find the words that start with a \"#\" sign in a string in java. There can be spaces between the sign and the word as well.

The string \"hi #how are # yo

5条回答
  •  醉话见心
    2021-02-08 22:02

    I think you may be best off using the split method on your string (mystring.split(' ')) and treating the two cases separately. Regex can be hard to maintain and read if you're going to have multiple people updating the code.

    if (word.charAt(0) == '#') {
      if (word.length() == 1) {
        // use next word
      } else {
        // just use current word without the #
      }
    }
    

提交回复
热议问题