shifting of characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tigger
    New Member
    • May 2007
    • 37

    #1

    shifting of characters

    i have a string which is extracted from a text file. from that particular string, i have assigned each of the letters in that string a particular value.

    how do i know the position of each characters in a string?

    because from that string, i have to search for 4 consecutive characters which has an assigned value of more than 1.0.

    after calculating the average of the 4 characters, i have to shift to the next character, one at a time and calculate its average.

    i cant figure out how to shift to the next character in the string.

    for eg....

    string: "aaaabbbccc"

    after extracting four characters, "abbb",

    how do i shift to take the next character, "c" or go backwards to take the previous character, "a"?
  • JonJacobs
    New Member
    • Aug 2007
    • 22

    #2
    What version of VB? I'm only familiar with vb.net.

    One thing you might try, if it is available in your version, is change the string to an array of characters, then you can access each character at will.

    Comment

    • hariharanmca
      Top Contributor
      • Dec 2006
      • 1977

      #3
      Originally posted by tigger
      i have a string which is extracted from a text file. from that particular string, i have assigned each of the letters in that string a particular value.

      how do i know the position of each characters in a string?

      because from that string, i have to search for 4 consecutive characters which has an assigned value of more than 1.0.

      after calculating the average of the 4 characters, i have to shift to the next character, one at a time and calculate its average.

      i cant figure out how to shift to the next character in the string.

      for eg....

      string: "aaaabbbccc"

      after extracting four characters, "abbb",

      how do i shift to take the next character, "c" or go backwards to take the previous character, "a"?
      you can use inStr method to do this

      for EX

      inStr(1,"aaaabb bccc","abbb") + 1
      will be 'c'
      (Code Not Tested.)

      Comment

      • fplesco
        New Member
        • Jul 2007
        • 82

        #4
        Originally posted by hariharanmca
        you can use inStr method to do this

        for EX

        inStr(1,"aaaabb bccc","abbb") + 1
        will be 'c'
        (Code Not Tested.)
        Try to search functions Instr() and Mid(). That's all you need. Im sorry if I can't give you a sample coz I got no time for now.

        Good luck!

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          For this one, I think the Mid() function might be the way to go.

          I'm only familiar with VB6, so I'll write for that version.

          Here's something quick, off the top of my head...
          [CODE=vb]
          Dim TheString As String
          Dim Char As String * 1
          Dim I As Integer
          Dim Checking As String
          Dim Total As Long, Average As Single

          For I = 1 To Len(TheString)
          Char = Mid(TheString, I, 1)
          V = AssignedValueOf (Char)
          If V > 1 Then
          Total = Total + V
          Checking = Checking & Char
          If Len(Checking) = 4 Then
          ' Do whatever with this string. And...
          Average = Total / 4
          Checking = ""
          End If
          Else
          Total = 0
          Checking = ""
          End If
          Next[/CODE]

          Comment

          • tigger
            New Member
            • May 2007
            • 37

            #6
            thanks all..

            i got the average part of the program working already.

            i just am unsure on how to locate the characters in the string.

            i've tried using the inStr function. but the inStr function only returns the first position of the character that i want. what if the character is repetitive in the string?

            for eg...

            SearchString = "XXpXXpPXPX P"
            'string to search in

            SearchChar = "P"
            ' search for "P"

            MyPos = InStr(1, SearchString, SearchChar, 0)

            the comparison will return the position 7, indicating the first position of where the "P" is.

            how do i locate the position of the remaining "P"s?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              The first parameter for Instr() function is the starting position. So if you want to loop through finding the next character each time, you just take the last position found, add 1, and use that as the starting point for the next search.

              Comment

              Working...