0

I am trying to remove elements from one CSV string with other CSV string :

Example:

String s1= "a,b,c,d,e,f,g,h,w,p,4,5,12,u,v"; /* Base String */

String s2="e,5,v";` /*(input string) This elements has to remove from above string S1 */

Expected Output String s1: "a,b,c,d,f,g,h,w,p,4,12,u";

I have Tried: For loop and remove elements and append back String.

I can to this by no. of ways, but i want to know if there is any library/method/utility available ?

3 Answers 3

1

try to use String::replaceAll with some regex like this :

String regexReplace = "[" + s2.replace(",", "") + "]";//result [e5v]
s1 = s1.replaceAll(regexReplace, "") // replace all character in class [e5v] with empty
        .replaceAll(",{2,}", ",")// replace all two or more comma with one comma
        .replaceAll("(^,|,$)", "");// in some cases replace comma in start or in the end

General solution (to match words and not letters) you can use this instead :

String regexReplace = "(" + s2.replace(",", "|") + ")";// result (e|5|v)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use StringUtils class from apache commons Reference

Comments

0

Try StringUtil from Spring

String s1= "a,b,c,d,e,f,g,h,w,p,4,5,12,u,v"; /* Base String */
String s2="e,5,v";
System.out.println(StringUtils.deleteAny(s1, s2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.