Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

startsWith and prefix finders  RSS feed

 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I have a driver program that creates an array of string objects, then passes it to a class. The class needs to take the array and determine if there is a common prefix among the string objects in the array. I've been told startsWith would be a good method to use here. Can anyone give me any ideas on how to proceed here? I only need to write the findPrefix method in the class. When it is called, it determines whether there is a common prefix, and if there is, it assigns the longest common prefix to a string. I'm just not sure how startsWith works. I've looked at the API docs, but I learn much more by seeing some different examples.

If I knew more about startsWith, I'd be able to determine whether I need a loop to go through the array, or whether I should take a different approach. Any thoughts?
[ August 26, 2005: Message edited by: Greg Roberts ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does that help ?
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure. The array is called allStrings[] and I need to compare every string in the array for a common prefix. I just still don't know how startsWith works.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you understand ? If the beginning characters of a String are identical to the String passed as an argument then startsWith returns true, otherwise it returns false.

e.g. "prefixRestOfString" begins with "prefix", therefore
"prefixRestOfString".startsWith("prefix")
returns true.

"prefixRestOfString" does not begin with "elephant", therefore
"prefixRestOfString".startsWith("elephant")
returns false.

[ August 26, 2005: Message edited by: Joanne Neal ]
[ August 26, 2005: Message edited by: Joanne Neal ]
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the line:
"prefixRestOfString".startsWith("prefix")

1. Is "prefixRestOfString" supposed to have quotes?

2. How can I, instead of comparing it to ("prefix"), compare it to the start of another string? Remember, I'm looking for common prefixes among many different strings.

Here is the way I coded the method without using startsWith. Would this work?

[ August 26, 2005: Message edited by: Greg Roberts ]
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I'd do it would be



Disclaimer - I just typed this in off the top of my head, so there may be syntax/logic errors but hopefully you get the general idea - assume the prefix is the first string in your list and then keep shortening it when you find a string that doesn't match
[ August 26, 2005: Message edited by: Joanne Neal ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you want to discover the common prefixes in a list?

Now we have a (possibly very large) map of candidate prefixes up to some number of characters long and how often they occurred. You can sort them on frequency and see how common they are.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!