Find the words start from a special character java

后端 未结 5 1653
梦毁少年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 21:59

    Here's a non-regular expression approach...

    1. Replace all occurrences of a # followed by a space in your string with a #

      myString.replaceAll("\s#", "#")

    2. NOw split the string into tokens using the space as your delimited character

      String[] words = myString.split(" ")

    3. Finally iterate over your words and check for the leading character

      word.startsWith("#")

提交回复
热议问题