Java replace replace all charecters with space except the matched string like test

0 votes
I have a string try = " THis is test programe &%^#%$,"

Now I want to replace all the other charecters to "" (blank) apart from "test". THe output should be "test".

Used the below : but dint succeed. ANy solution please.

try.replaceAll("[ ^" + try +"]" ,"");  -- did not give the expected output
Oct 14, 2020 in Java by Jyra
• 580 points
769 views

1 answer to this question.

0 votes
Best answer

Hi, @jyra,

You can go through this example: 

String word = "THis is test programe &%^#%$";

String replaced = word.replace("work", "test");

System.out.println("Original String before replace : " + word);

System.out.println("Replaced String : " + replaced);



 

answered Oct 14, 2020 by Gitika
• 65,890 points

selected Oct 15, 2020 by Jyra
Thanks Gitika, Worked perfect...

@Jyra,

Glad it helped you to solve your query!!! Do post the queries on technologies you are working on, would be happy to help!!!

Related Questions In Java

0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
2,233 views
0 votes
1 answer

Replace String with another in java

String s = "HelloSuresh"; s = s.replace("Hello",""); System.out.pri ...READ MORE

answered Aug 21, 2018 in Java by Parth
• 4,640 points
831 views
0 votes
1 answer
0 votes
1 answer

Replace all 0's with 5 in Java

Take a look at this, hope this ...READ MORE

answered Mar 6, 2019 in Java by Priyaj
• 58,100 points
6,299 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,095 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,308 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,381 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,813 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,434 views
+1 vote
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,520 points
209,466 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP