9 Replies - 525 Views - Last Post: 04 December 2012 - 09:47 AM Rate Topic: -----

#1 methane95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 16-February 12

How do I find the index for each letter in a word?

Posted 03 December 2012 - 01:31 PM

For example:
dim word as string = "apple"
dim index as integer
index = word.IndexOf("p")



This code only returns me with 1 which is only the index for the first p not both of them. What I need the program to do is find the index for both p's and return with a value similar to (1,2)
Is This A Good Question/Topic? 0
  • +

Replies To: How do I find the index for each letter in a word?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: How do I find the index for each letter in a word?

Posted 03 December 2012 - 01:37 PM

You would need to make a function that returns that custom output... the function could be as simple as a for-loop that checks each character and returns an array of indexes, or some other complex-ish array traversal.
Was This Post Helpful? 0
  • +
  • -

#3 methane95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 16-February 12

Re: How do I find the index for each letter in a word?

Posted 03 December 2012 - 01:45 PM

View Postmodi123_1, on 03 December 2012 - 01:37 PM, said:

You would need to make a function that returns that custom output... the function could be as simple as a for-loop that checks each character and returns an array of indexes, or some other complex-ish array traversal.

I know how to do that but isn't there a "one liner" for it because that just seems too much to do for such a simple task?

This post has been edited by methane95: 03 December 2012 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9277
  • View blog
  • Posts: 34,789
  • Joined: 12-June 08

Re: How do I find the index for each letter in a word?

Posted 03 December 2012 - 01:47 PM

Have you checked over the MSDN string functions?

http://msdn.microsof...y/7wtc81z6.aspx

... but not, there is no "give me the none to n indexes of this character in this string".
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: How do I find the index for each letter in a word?

Posted 03 December 2012 - 01:58 PM

There is a one liner but it involves a little thinking.
Hint:- It involves .SelectMany .
Was This Post Helpful? 0
  • +
  • -

#6 methane95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 16-February 12

Re: How do I find the index for each letter in a word?

Posted 03 December 2012 - 02:09 PM

View PostAdamSpeight2008, on 03 December 2012 - 01:58 PM, said:

There is a one liner but it involves a little thinking.
Hint:- It involves .SelectMany .

Doesn't that require me to create a function?
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How do I find the index for each letter in a word?

Posted 03 December 2012 - 05:07 PM

View Postmethane95, on 03 December 2012 - 03:09 PM, said:

Doesn't that require me to create a function?

I haven't used Linq at all, but no matter. Even if it does require a Function or Sub, what's the big deal? Functions and Subs are your friends.

        Dim ch As New List(Of String)
        For Each c In "apple"
            ch.Add(c)
        Next
        For i = 0 To ch.Count - 1
            Debug.Print(ch(i))
        Next


Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: How do I find the index for each letter in a word?

Posted 04 December 2012 - 01:54 AM

There is another oneliner, involving selecting those indexes from Enumerable and its Where extension.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: How do I find the index for each letter in a word?

Posted 04 December 2012 - 09:25 AM

You'll have to write a small function. Functions and Subroutines ain't a bad thing. They allow you to break the problem do into discrete sections of functionality. And allow you reuse those sections elsewhere.

Dim indice=word.SelectMany(Function(letter, index) If(letter=choosen,{index},{})

This post has been edited by AdamSpeight2008: 04 December 2012 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#10 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: How do I find the index for each letter in a word?

Posted 04 December 2012 - 09:47 AM

...or:
Dim indexes() as integer = Enumerable.Range(0,word.length).Where(Function(index) word(index) = "p").ToArray


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1