PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Replacing a string with index of an array-VBForums
Results 1 to 2 of 2

Thread: Replacing a string with index of an array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    106

    Replacing a string with index of an array

    Hi all, i have an array that contains a-z.
    When i enter text like "ab" into a textbox and click the button, it will search the array and replace the text accordingly to the index number of the array and rewrite into the textbox with the index number.

    But what i got from the code below is "ab0 1".

    May i know how to do so that the textbox will only display the index number and not with the letters.

    Also, how do i search the array from index 12 onwards to the end of the array. Thanks
    Code:
       For Each b As String In TextBox2.Text
    
                If abc.Contains(b) Then
                    Dim ab As Integer = Array.IndexOf(abc, b)
    
                    b = Replace(LCase(b), b, ab & " ")
                    TextBox2.Text &= b
    
                End If
            Next
    Last edited by hsienfoo; Jul 12th, 2010 at 06:32 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,859

    Re: Replacing a string with index of an array

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim abc(25) As Char
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim chars As String = TextBox2.Text
    7.         TextBox2.Text = ""
    8.         For Each b As Char In chars
    9.             If abc.Contains(Char.ToLower(b)) Then
    10.                 Dim ab As Integer = If(Not CheckBox1.Checked, Array.IndexOf(abc, b), Array.IndexOf(abc, b) + 12)
    11.                 TextBox2.Text &= ab
    12.             End If
    13.         Next
    14.     End Sub
    15.  
    16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    17.         For x As Integer = 65 To 90
    18.             abc(x - 65) = Char.ToLower(Chr(x))
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    23.         If CheckBox1.Checked Then 'search from index 12 onwards
    24.             ReDim abc(13)
    25.             For x As Integer = 77 To 90
    26.                 abc(x - 77) = Char.ToLower(Chr(x))
    27.             Next
    28.         Else 'search from a-z
    29.             ReDim abc(25)
    30.             For x As Integer = 65 To 90
    31.                 abc(x - 65) = Char.ToLower(Chr(x))
    32.             Next
    33.         End If
    34.     End Sub
    35. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width