This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using contains/equals/== with random characters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using contains/equals/== with random characters?" Watch "Using contains/equals/== with random characters?" New topic
Author

Using contains/equals/== with random characters?

David Phluphy
Greenhorn

Joined: Sep 09, 2010
Posts: 25
Hi!

I'm wondering if there's anything I can use to check if my string array contains a word with a random character.

Something like "if(stringArray[i] == st*ing), where * can be any character from a to z ?
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

David Phluphy wrote:Hi!

I'm wondering if there's anything I can use to check if my string array contains a word with a random character.

Something like "if(stringArray[i] == st*ing), where * can be any character from a to z ?

If you compare String, you should use equals(), not ==
How about using regular expression, st[a-z]ing
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

David Phluphy wrote:Something like "if(stringArray[i] == st*ing), where * can be any character from a to z ?


remove 2nd index character from your input string stringArray[i] and then compare "sting".equals(refactorStringArray[i])
(or)
you can also use regular expression


[edit]beaten by Raymond[/edit]
hth
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
The regex solution given by Seetha is simpler and it can be called directly on the String as well


use [a-z] if you want to check for small caps or [A-Z] for caps or [a-zA-Z] if case does not matter


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
David Phluphy
Greenhorn

Joined: Sep 09, 2010
Posts: 25
Thanks for your replies so far. I've tried them all and failed gloriously each time. I should have explain the problem in more detail.

I have a method findWord(String word, String[] dictionary), which is supposed to return the index of the word in the dictionary[].

The word can either be a full word, in which case it's super simple, but it can also contain a arbitrary amount of random characters.

Let's say word1 = **z**, word2 = p*z**, word3 = piz**, word4 = piz*a and word5 = pizza.

All of these words would return 5 if dictionary[5] was "pizza"

I think this is harder than I thought at first...
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Well, if this the scenario - then try exploring more of Regular Expressions

Here is a crude code that would work for the scenario you have mentioned - try refining the search algorithm and you can explore other classes as well.

Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Well, if this the scenario - then try exploring more of Regular Expressions

Here is a crude code that would work for the scenario you have mentioned - try refining the search algorithm and you can explore other classes as well.



Here I am assuming that each * actually stands for one random character and that random character could be between a-z or between A-Z. You can match other options as well - check java.util.regex.Pattern class' API for more details on regex patterns.
David Phluphy
Greenhorn

Joined: Sep 09, 2010
Posts: 25
That worked brilliantly. Good thing I didn't spend five hours trying to figure this out before posting ... oh, wait
Thanks a bunch though
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Hey David,

I am glad that this worked. However, I suggest you test this for other combination and though this would work fine for smaller arrays, try checking it's performance for larger ones as well.

Good luck
David Phluphy
Greenhorn

Joined: Sep 09, 2010
Posts: 25
I already did.. it was absolutely horrible for larger sets
I can't figure out a way to do this fast
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using contains/equals/== with random characters?
 
Similar Threads
validate form fields using javascript in struts
String Manipulation
NEed Help!
how to extract two numbers from a string
Store Vector in MySql database