Combining replaceAll String | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Combining replaceAll String

Hi, I like to know from my attached code, how do I use replaceAll method in one line instead of creating another string? https://code.sololearn.com/cxb375wCJMSQ/?ref=app

19th Dec 2022, 10:03 AM
Sibusiso Mbambo
4 Answers
+ 5
import java.util.Arrays; public class Program { public static void main(String[] args) { int a[] = {1, 2, 3, 4, 5, 6}; String star = Arrays.toString(a).replaceAll("\\[|\\]|,|\\s", "").replace("2", "").replace("", ","); System.out.println(star); //System.out.println(newStar); // System.out.println(newStarNew); // combining as stream star = Arrays.toString(a).replaceAll("\\[|\\]|,|\\s2","").replace(" ", ","); System.out.println(star); // single substring regex.. star = Arrays.toString(a).replaceAll("\\[|\\]|\\s|2,",""); System.out.println(star); } } // 1,3,4,5,6 is what you trying I think..
19th Dec 2022, 11:47 AM
Jayakrishna 🇮🇳
+ 2
Thank you
19th Dec 2022, 12:39 PM
Sibusiso Mbambo
+ 1
Where is code attached?
19th Dec 2022, 10:52 AM
Jayakrishna 🇮🇳
+ 1
Sorry, now attached!
19th Dec 2022, 11:17 AM
Sibusiso Mbambo