another simple Java question: string search

HardTech

Golden Member
Oct 9, 1999
1,203
0
76
Say I have two strings that say "American Dollars" and "Australian Dollars"

I make another string that says "Dollar"... is there a way I can compare "Dollar" to the two strings above and have it return a true?

is there a partial match method in the String class?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
You could use indexOf() or matches() (if you want a regular expression search).
 

HardTech

Golden Member
Oct 9, 1999
1,203
0
76
sweet... I would have never guessed that looking at Sun's website.

Thanks a bunch!!
 

HardTech

Golden Member
Oct 9, 1999
1,203
0
76
hmm.. it's not working

so let's say:

String USD = "American Dollars";
String AUD = "Australian Dollars";
String HardTech = "Dollars";

HardTech.indexOf(USD) would return a value >= 1?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: HardTech
hmm.. it's not working

so let's say:

String USD = "American Dollars";
String AUD = "Australian Dollars";
String HardTech = "Dollars";

HardTech.indexOf(USD) would return a value >= 1?

you want: USD.indexOf(HardTech);
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: HardTech
hmm.. it's not working

so let's say:

String USD = "American Dollars";
String AUD = "Australian Dollars";
String HardTech = "Dollars";

HardTech.indexOf(USD) would return a value >= 1?

No, but USD.indexOf(HardTech) should. Read the documentation for that method again.
 

znaps

Senior member
Jan 15, 2004
414
0
0
You can also say

USA.endsWith(HardTech)

Which would return a boolean value of true.

P.S don't name local variables starting with a capital letter, it's breaks the conventional approach which would be hardTech.