Results 1 to 5 of 5

Thread: Splitting String

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    3

    Splitting String

    I am experiencing a problem with splitting a two digit integer into two one digit integers (e.g. 17 = 1,7) the string.split variable doesn't seem to work as there is nothing between the two digits of the integer. Any help as to what I can use would be greatly appreciated, many thanks in advance.


    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnInfo.Click
    
            Dim SplitCV As Integer
            Dim SplitCVSur As Integer
            Dim NextIntCV(2) As Integer
            Dim Output As Integer
            Dim i As Integer
            Dim NextStrCV() As String
            Dim StrNCV As String
    
            For Each C In txtFirst.Text
    
                If C = "A" Or C = "J" Or C = "S" Then
                    SplitCV = SplitCV + 1
                ElseIf C = "B" Or C = "K" Or C = "T" Then
                    SplitCV = SplitCV + 2
                ElseIf C = "C" Or C = "L" Or C = "U" Then
                    SplitCV = SplitCV + 3
                ElseIf C = "D" Or C = "M" Or C = "V" Then
                    SplitCV = SplitCV + 4
                ElseIf C = "E" Or C = "N" Or C = "W" Then
                    SplitCV = SplitCV + 5
                ElseIf C = "F" Or C = "O" Or C = "X" Then
                    SplitCV = SplitCV + 6
                ElseIf C = "G" Or C = "P" Or C = "Y" Then
                    SplitCV = SplitCV + 7
                ElseIf C = "H" Or C = "Q" Or C = "Z" Then
                    SplitCV = SplitCV + 8
                ElseIf C = "I" Or C = "R" Then
                    SplitCV = SplitCV + 9
                End If
            Next C
    
            For Each C In txtSur.Text
                If C = "A" Or C = "J" Or C = "S" Then
                    SplitCVSur = SplitCVSur + 1
                ElseIf C = "B" Or C = "K" Or C = "T" Then
                    SplitCVSur = SplitCVSur + 2
                ElseIf C = "C" Or C = "L" Or C = "U" Then
                    SplitCVSur = SplitCVSur + 3
                ElseIf C = "D" Or C = "M" Or C = "V" Then
                    SplitCVSur = SplitCVSur + 4
                ElseIf C = "E" Or C = "N" Or C = "W" Then
                    SplitCVSur = SplitCVSur + 5
                ElseIf C = "F" Or C = "O" Or C = "X" Then
                    SplitCVSur = SplitCVSur + 6
                ElseIf C = "G" Or C = "P" Or C = "Y" Then
                    SplitCVSur = SplitCVSur + 7
                ElseIf C = "H" Or C = "Q" Or C = "Z" Then
                    SplitCVSur = SplitCVSur + 8
                ElseIf C = "I" Or C = "R" Then
                    SplitCVSur = SplitCVSur + 9
                End If
            Next C
    
            lblName1.Text = SplitCV
            lblName2.Text = SplitCVSur
    
            If SplitCV = 0 Then
                Console.WriteLine("Digit too small please try again")
            End If
    
            StrNCV = Str(SplitCV)
    
            NextStrCV = Str(StrNCV).Split(" ")
    
            For Each i In NextStrCV
                Output = Output + i
            Next i
    
    
    
            lblInstructions.Text = Str(Output)
    
        End Sub
    End Class

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: Splitting String

    Try using Substring method

    Code:
            StrNCV = SplitCV.ToString
    
            For i = 0 To StrNCV.Length - 1
                Output = Output + CInt(StrNCV.Substring(i, 1))
            Next i
    
            lblInstructions.Text = Output.ToString



  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Splitting String

    Strings can be treated as arrays of Char, or you can use the Chars property to get the array.

    For example:
    Code:
    Dim value As String = "17"
    For i As Integer = 0 To value.Length - 1
      Console.WriteLine(value(i))
    Next
    
    Dim digits() As Char = value.Chars
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,837

    Re: Splitting String

    I'd go straight for the Chars array...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    3

    Re: Splitting String

    Thanks all, you were all really helpful

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width