1

someone can help me with code? How to search word in String text, this word end "." or "," in java

I don't want search like this to find it

String word = "test.";

String wordSerch = "I trying to tasting the Artestem test.";

String word1 = "test,"; // here with ","

String word2 = "test."; // here with "."

String word3 = "test";  //here without 

//after i make string array and etc...

if((wordSearch.equalsIgnoreCase(word1))||
   (wordSearch.equalsIgnoreCase(word2))||
   (wordSearh.equalsIgnoreCase(word3))) {
}

if (wordSearch.contains(gramer)) 
//it's not working because the word Artestem  will contain test too, and I don't need it
Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92
Ahmad Al ALloush
  • 330
  • 3
  • 10
  • Have you done some research? Based on your needs, `contains`, `startWith`, ... from String or a Regex will do it, there is enough existing source I believe to find what you need – AxelH Feb 15 '17 at 13:51
  • Your question doesn't make much sense. You may find `String.endsWith()` useful though. – Steve Smith Feb 15 '17 at 13:51
  • What about using regex (as pointed by AxelH)? http://stackoverflow.com/questions/9464261/how-to-find-the-exact-word-using-a-regex-in-java – RubioRic Feb 15 '17 at 13:52
  • Explain what you want because this is not clear. Adding some example is always a good idea. But I am pretty sure this is a simple search that could be answer by Google – AxelH Feb 15 '17 at 13:55
  • thank you The complex question :) My second language is English so sorry for my words non-clear I am researching within the text through the words stored in a Array, so looking for a way to shorten the code – Ahmad Al ALloush Feb 15 '17 at 18:57

5 Answers5

1

You can use the matches(Regex) function with a String

String word = "test.";
boolean check = false;
if (word.matches("\w*[\.,\,]") {
    check = true;
}
CloudPotato
  • 1,255
  • 1
  • 17
  • 32
1

You can use regex for this

Matcher matcher = Pattern.compile("\\btest\\b").matcher(wordSearch);
if (matcher.find()) {
}

\\b\\b will match only a word. So "Artestem" will not match in this case. matcher.find() will return true if there is a word test in your sentence and false otherwise.

Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69
1
String stringToSearch = "I trying to tasting the Artestem test.   test,";

Pattern p1 = Pattern.compile("test[.,]");
Matcher m = p1.matcher(stringToSearch);

while (m.find())
{
    System.out.println(m.group());   
}
Avaneesh
  • 162
  • 5
0

What is a word? E.g.:

  1. Is '5' a word?
  2. Is '漢語' a word, or two words?
  3. Is 'New York' a word, or two words?
  4. Is 'Kraftfahrzeughaftpflichtversicherung' (meaning "automobile liability insurance") a word, or 3 words?

For some languages you can use Pattern.compile("[^\\p{Alnum}\u0301-]+") for split words. Use Pattern#split for this.

I think, you can find word by this pattern:

String notWord = "[^\\p{Alnum}\u0301-]{0,}";
Pattern.compile(notWord  + "test" + notWord)` 

See also: https://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Mikhail Ionkin
  • 568
  • 4
  • 20
0

You can transform your String in an Array divided by words(with "split"), and search on that array , checking the last character of the words(charAt) with the character that you want to find.

String stringtoSearch = "This is a test.";
String whatIwantToFind = ",";

String[] words =  stringtoSearch.split("\\s+");
for (String word : words) {
    if (whatIwantToFind.equalsignorecas(word.charAt(word.length()-1);)) {
        System.out.println("FIND");

    }
}
Abraham
  • 189
  • 1
  • 18