• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Using contains/equals/== with random characters?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Maybe he went home and went to bed. And took this tiny ad with him:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic