Question

Is it possible to compare strings to arrays in android? This is for the dictionary application using the Porter Stemmer that i am developing for my thesis.

For example the input word is PROCESSES (placed inside a char array), and I'd like to scan if the input contains "s","es" or "sses" then delete them.

I thought of comparing letter by letter but it would be a tedious coding I believe. Is there an easier way to just compare the input word like so:

If ( inputWord has string "es" )
 {
   delete "es
 }

example input: PROCESSES
example output: PROCESS
Was it helpful?

Solution

if(inputWord.contains(checkWord))
{
    int i = inputWord.indexOf(checkWord);
    StringBuilder sb = new StringBuilder(inputWord);
    sb = sb.delete(i, i+(checkWord.length()));
    inputWord = sb.toString();
}

Store your values in String. It will be more easier. Hope this code will help you. If you have to check multiple words, store all words in a array, and put this snippet inside a for loop.

OTHER TIPS

You can use the String.contains and String.replaceAll functionality:

//if (inputWord.contains("es")) {
    inputWord.replaceAll("es", "");
//}

You don't need the if statement, you can also do directly a replace. If the string is not found it won't do anything.

Perhaps I don't understand your question, but you can convert between String and char[] using

char[] data = {'a', 'b', 'c'};
String str = new String(data);

and

String str = "abc";
char[] data = toCharArray();

You can check whether a substring is contained using indexOf(String) or contains(String).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top