We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,459 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

exact string match

hey,
Iv been looking around for an easy way to check if a string matches exactly and not just in a few characters I.E

B != "ABC"
B == B
Frederick != F

So only if the entire string matches will my condition be met. currently im using :

               String[] splitstring = linetoscan.split("[ ]+");
               //split entire input by spaces

              for(int j=0;j<splitstring.length;j++)
              {
                if(splitstring[j].contains("F"))
                {
                System.out.println("match found");
                }
              }

I read that the pattern matcher can be used, but seems a lot of conversion has to be done to compare a string to a pattern of characters? is pattern matcher the most light weight way to achieve this goal?

3
Contributors
2
Replies
19 Hours
Discussion Span
10 Months Ago
Last Updated
3
Views
trishtren
Junior Poster in Training
79 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Do you mean string1.equals(string2) ?
(returns true only if both strings contain exactly the same sequence of characters)

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

or, for the String class, you also have the equalsIgnoreCase method, which will return true for both:

String a = "aBc";
a.equalsIgnoreCase("aBc");
a.equalsIgnoreCase("abc");

when you want to compare values of Objects, as JamesCherrill pointed out, you'll need the equals method. Only if you want to compare values of primitive types, you should use '==' or '!='.

stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 627
Skill Endorsements: 25

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0700 seconds using 2.66MB