Extract words starting with a particular character from a string
I got the following string: String line = "#food was testy. #drink lots of. #night was fab. #three #four"; I want to take #food #drink #night #three and #four from it. I tried this code: String[] words = line.split("#"); for (String word: words) { System.out.println(word); } But it gives food was testy , drink lots of , nigth was fab , three and four . Orace split will only cuts the whole string at where it founds a # . That explain your current result. You may want to extract the first word of every pieces of string, but the good tool to perform your task is RegEx Here how you can achieve it: