Hey there! any question in your mind? Ask It Now!.

Popular Categories





How to Get Some Specific Characters From a String In Java?

+2 votes
788 views
asked in Programming by nisha (720 points)
edited by nisha
Hi All, I have a Big String which is dynamic and want get some spacific characters from that String.
 
Like I have a String strMain = "My country name is India"
 

here I want to check what is the country name . Because this string is dynamic, I mean some time the string becomes

New country is USA

and some time My country is Poland. It comes with different-2 sentence . so its difficult for me to compare country name with another string. so please suggest any logic for the same.

 

Thanks For helping me.

 

2 Answers

+1 vote
answered by Anand
Please look into classes like Pattern and Matcher.

Here the idea is to device the string as regex like :

My country name is (\S+) and user matcher to capture matching group.
0 votes
answered by Bhartesh (2,690 points)
edited by Bhartesh

You can use java.lang.String.contains() method .

You have string strMain so you can check secuence of substrings and can compare those sub strings with your dynamic result like 
 

String strCountry = "India";

boolean  Finalstr = strMain.contains("+strCountry+");

Because this method return true or false so by Finalstr you can varify dynamic string contains that India or same as other countries  or not.

I hope it would help you.

Related Questions

+3 votes
1 answer 614 views
+6 votes
2 answers 2,068 views
–1 vote
0 answers 108 views
+2 votes
1 answer 141 views
+6 votes
1 answer 45 views
+3 votes
1 answer 355 views
+1 vote
1 answer 85 views

Not a Member yet?

Ask to Folks Login

My Account
347 Folks are online
27 members and 320 guest online
Your feedback is highly appreciated