Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In java, how to append string ignoring duplicates

Tags:

java

I have string say "ABC,D" , now I wish to write a method append(initialStr, currStr ) which appends currStr to initailStr only if currstr is not already present in initialStr. I tried a method which splits with comma, but since my string contains comma so that method doesn't works for me. Any help will be greatly appreciated.

like image 603
Ravi Gupta Avatar asked Mar 01 '23 06:03

Ravi Gupta


1 Answers

String appendIfNotPresent(String initial, String curr)
{
   if (initial.contains(curr))
     return initial;
   else
     return initial + curr;
}
like image 161
Ali Avatar answered Mar 12 '23 13:03

Ali