• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using contains/equals/== with random characters?

 
David Phluphy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Phluphy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already did.. it was absolutely horrible for larger sets
I can't figure out a way to do this fast
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic