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
93 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,870 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
• 58,990 points
348 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,630 points
158 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
2,297 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 6, 2018 by Omkar 38,073 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,520 points
5,017 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,520 points
11,389 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,520 points
2,160 views
+15 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
210,091 views
0 votes
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,420 points
60,638 views