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 in Java by Jyra
• 580 points
43 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 by Gitika
• 48,480 points

selected Oct 15 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,210 points
312 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
136 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
• 57,700 points
2,035 views
+5 votes
3 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 33,237 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
4,640 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
10,720 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
1,985 views
0 votes
1 answer

Convert data in List<JsonNode> to Map <String,String> in Java

Hi, @Jyra, Basically, you can use the object mapper to ...READ MORE

answered Apr 7 in Java by Gitika
• 48,480 points
2,901 views
+1 vote
1 answer

Multithreading in Java with priorities

HI, @Juan, By default, a thread inherits the priority of its parent thread. ...READ MORE

answered Nov 6 in Java by Gitika
• 48,480 points
48 views