Black Swan Black Swan - 24 days ago 6
Java Question

How to skip a particular string during string Joining in Java?

The source code I have uploaded it will join some strings value in a one line. I want a way that I can able to skip a particular string in time of string joining. Here i have stored the strings "This","is","a","test." in a string array. I want that in time of joining a particular string will be skipped. Like I want to skip "a". How can I able to do in Java? I want a generalized way that I will able to apply for any strings.

import java.util.StringJoiner;
public class Test_Joiner_for_seatPlan
{
public static void main(String[] args)
{
StringJoiner joinString = new StringJoiner( " ");
String[] testStringArray = {"This","is","a","test."};

String joinedString = null;

for(String s : testStringArray)
{
joinString.add(s);
}

joinedString = joinString.toString();

System.out.println("After Joining the String:\n"+joinedString);
}
}

Answer

Try with not equal condition with string. but its not feasible as its check everytime a value.

for(String s : testStringArray)
{
  if(!s.equals("a")){
      joinString.add(s);
   }
}

If you have a list of values like a,b,c than you can do like this:

 Set<String> exclude =  new HashSet<String>(Arrays.asList("a","b","c"));
 for(String s : testStringArray)
    {
      if(!exclude.contains(s)){
          joinString.add(s);
       }
  }