Results 1 to 12 of 12

Thread: How to determine 2 Values of Splitted String Array into One String Array for specific

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    How to determine 2 Values of Splitted String Array into One String Array for specific

    Hello

    Would it be possible to determine Two Values of splitted array into one string array if yes then how ? For this Specific case

    Code:
    Dim strComDetailLine As String = "Communication detail           customercare@abcdFtech.com admins@abcdFtech.com " 
    
    Dim strComDetailLineReplaced As String
    Dim arrStrComDetailsLine() As String
    
        strComDetailLineReplaced = Regex.Replace(strComDetailLine, "\s+", " ")
    
        TextBox6.Text = strComDetailLine
        TextBox7.Text = strComDetailLineReplaced  
        
        arrStrComDetailsLine = Split(strComDetailLineReplaced, " ")
        TextBox8.Text = arrStrComDetailsLine(0)   
        TextBox9.Text = arrStrComDetailsLine(1)   
        TextBox10.Text = arrStrComDetailsLine(2)
    VALUES DISPLAYED BELOW with above coding
    arrStrComDetailsLine(0) = "Communication"
    arrStrComDetailsLine(1) = "Detail"
    arrStrComDetailsLine(2) = "customercare@abcdFtech.com"
    arrStrComDetailsLine(3) = "admins@abcdFtech.com "
    Infact i would like if i could get the Value as mentioned below
    arrStrComDetailsLine(0) = "Communication Detail"
    arrStrComDetailsLine(1) = "customercare@abcdFtech.com"
    arrStrComDetailsLine(2) = "admins@abcdFtech.com "
    Your valuable Input will be appreciated

    Thanks
    SamD
    159

  2. #2
    Addicted Member
    Join Date
    Jul 2022
    Posts
    170

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    Is the format of the string known? Will it always have a large amount of spaces between the first and second segments?
    If so then I'd do something like this:

    Code:
            Dim strComDetailLine As String = "Communication detail           customercare@abcdFtech.com admins@abcdFtech.com "
    
            Dim strComDetailLineReplaced As String
            Dim arrStrComDetailsLine() As String
    
            Dim strHeading As String = strComDetailLine.Substring(0, strComDetailLine.IndexOf("  "))
    
            ' don't need regex can just use trim to remove the spaces before and after the left over string
           'strComDetailLineReplaced = Regex.Replace(strComDetailLine.Substring(strComDetailLine.IndexOf("  ")), "\s+", " ")
            'arrStrComDetailsLine = strComDetailLineReplaced.Trim.Split(" ")
    
            strComDetailLineReplaced = strComDetailLine.Substring(strComDetailLine.IndexOf("  ")).Trim()
    
            arrStrComDetailsLine = strComDetailLineReplaced.Split(" ")
    EDIT: removed the regex part as it isn't needed this way
    Attached Images Attached Images  
    Last edited by jdelano; Dec 2nd, 2023 at 05:54 AM.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,313

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    1) look for first @
    2) from there go back until you hit first space.
    3) everything left there is line 0 (trim out whitespace)
    4) everything right from there split along space
    5) done
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,090

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    I think a pertinent question is, what is the source of this String to begin with? Your example code has you hard-coding a String, presumably as a simple example for us here.

    But I'm assuming that in practice, this String is coming from some external source (user input, etc). If it is other code of yours that is creating this string in this format (and thus creating this problem for yourself), then there are other options available on the "front end" of this String creation that can be done that eliminates this problem.

    One example, don't separate fields with spaces if there may be spaces found within the data in any of the fields.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    jdelano

    You have asked not to use Regex as this is not needed

    But appreciate your input in showing the 2 Options

    One with Regex.Replace....Substring and another without Regex.Replace
    Code:
    strComDetailLineReplaced = Regex.Replace(strComDetailLine.Substring(strComDetailLine.IndexOf("  ")), "\s+", " ")
    OR
    strComDetailLineReplaced = strComDetailLine.Substring(strComDetailLine.IndexOf("  ")).Trim()
    Which is the better Option from above 2 options ?

    Another Question

    Why there are Two Spaces in .IndexOf(" ")

    OB1
    Dude you wiil not spare me for (pl click on Red example) below
    Your example code has you hard-coding a String, presumably as a simple example for us here.
    therefore
    Code:
    Dim strComDetailLine As String = "Communication detail           customercare@abcdFtech.com admins@abcdFtech.com "
    OR
    Dim NameOfBasicStruFile As String = "C:\TRIAL\NEWSTRU.TXT"
    
    Dim strComDetailLine As String = readNthLine(NameOfBasicStruFile, 10)
    One example, don't separate fields with spaces if there may be spaces found within the data in any of the fields.
    BTW fields are separated when file created in this case. But will take care. Thanks for the precaution

    SamD
    160
    Last edited by SamDsouza; Dec 2nd, 2023 at 11:42 PM.

  6. #6
    Addicted Member
    Join Date
    Jul 2022
    Posts
    170

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    I used two spaces to indicate where the first segment is, as there are a large number of spaces after it, whereas the remaining text is delimited by a single space.

    If we know this is the format that will be received, then we don't need to use regex to simply trim the empty spaces away.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,090

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    Quote Originally Posted by SamDsouza View Post
    Code:
    Dim strComDetailLine As String = "Communication detail           customercare@abcdFtech.com admins@abcdFtech.com "
    OR
    Dim NameOfBasicStruFile As String = "C:\TRIAL\NEWSTRU.TXT"
    
    Dim strComDetailLine As String = readNthLine(NameOfBasicStruFile, 10)
    BTW fields are separated when file created in this case. But will take care. Thanks for the precaution

    SamD
    160
    Thank you for clarifying. The data is being read in from an external text file and that is how the data looks. Are these text files created by some other process that is outside of your control? Or do you have other code of yours that you have written that is creating these text files formatted this way?

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,313

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    I don't understand why you're making such Rocket science out of this simple Problem

    This was done in VBA, so might have to adapt to .NET
    Instead of the Result-Array assign directly to your textboxes
    Code:
    Sub Something()
    Dim Result(0 To 2) As String
    Dim s As String
    Dim i As Long
    Dim j As Long
        s = "Communication detail           customercare@abcdFtech.com admins@abcdFtech.com "
        s = Trim$(s)    'Remove leading/trailing whitespace
        'Look for first Space from the right
        i = InStrRev(s, " ")
        'Second Mail-Address from there to the end
        Result(2) = Trim$(Mid$(s, i + 1))
        'Look for second Space from the right
        j = InStrRev(s, " ", i - 1)
        'First Mail-Address from there to the previous
        Result(1) = Trim$(Mid$(s, j + 1, i - j))
        'Everything left from there is....
        Result(0) = Trim$(Left$(s, j))
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2021
    Posts
    172

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    Hello guys

    in fact error came in scenario where
    Code:
    Dim strComDetailLine As String = "Communication detail"
    
    OR
    Dim NameOfBasicStruFile As String = "C:\TRIAL\NEWSTRU.TXT"
    Dim strComDetailLine As String = readNthLine(NameOfBasicStruFile, 10)
    Dim strHeading As String = strComDetailLine.Substring(0, strComDetailLine.IndexOf("  "))' System.ArgumentationOutOfRangeException:
    'Length cannot be less than zero
    
    in the below Following Scenario
    "Communication detail"
    So how Could i prevent the above Error

    As explored on indexOf and LastIndex of but this is going above my head.

    And what if Scenario are as below too and bit difficult to predict How may email address will pop up in single Line or No Email address at all or more than 2
    if some concrete syntaxes can come up will clear my doubts
    By the way all Email IDS are starting from position 32

    Zvoni Thanks for Workout in VBA. At present it is for Vb.net

    Thanks
    SamD
    161
    Last edited by SamDsouza; Dec 5th, 2023 at 08:27 AM.

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,313

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    To Find out, how many email-addresses are in the inputstring, just split along "@".
    Resulting Array-Length minus 1 is the amount of mail-addresses (Or just the UBound-Value of the resulting Array of the Split)
    From there it's just math using my algorithm above

    Code:
    Sub Something()
    Dim Result() As String
    Dim a() As String
    Dim s As String
    Dim i As Long
    Dim j As Long
    Dim c As Long           'Count of mail-addresses
        s = "Communication detail       " '     customercare@abcdFtech.com admins@abcdFtech.com info@abcdFtech.com "
        s = Trim$(s)        'Remove leading/trailing whitespace
        a = Split(s, "@")   'Get the count of mail-addresses
        c = UBound(a)       'Count of mail-addresses
        ReDim Result(0 To c)
        If c > 0 Then       'Only entering if at least one mail-address
            j = Len(s) + 1
            Do
                i = InStrRev(s, " ", j - 1)
                Result(c) = Trim$(Mid$(s, i + 1, j - i))
                c = c - 1
                j = i
            Loop Until c = 0
            Result(0) = Trim$(Left$(s, i))
        Else
            Result(0) = Trim$(s)
        End If
    End Sub
    Convert above Sub to a Function which returns a String-Array......

    EDIT:
    Zvoni Thanks for Workout in VBA. At present it is for Vb.net
    I know that, since we are in the VB.NET-Subforum.

    I'm sure you can adapt my code for VB.NET, since i don't have a clue about .NET (never programmed a single line of code in it)
    Last edited by Zvoni; Dec 5th, 2023 at 09:20 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11
    Addicted Member
    Join Date
    Jul 2022
    Posts
    170

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    You should check for the existance of the two spaces first, or if there are no @'s in the string. Then you know you don't have to split up the data at all.

    Something like
    Code:
        If strComDetailLine.IndexOf("  ")) > 0 then 
            ' only use the location if it is there, else
            strHeading = strComDetailLine.Substring(0, strComDetailLine.IndexOf("  "))
        Else
            strHeading = strComDetailLine
        End If
    you'll need to code for the known permiations and also the unknown by making sure the string fits in the formats you expect.

  12. #12
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,029

    Re: How to determine 2 Values of Splitted String Array into One String Array for spec

    here a sample to Filter the Emails, not sure what the other part means is with ..Communication detail
    I just added it to find that and the next word

    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim s As String = "    Communication detail       customercare@abcdFtech.com admins@abcdFtech.com info@abcdFtech.com "
            'Filter all Emails from string;
            Dim theRegex As New Regex("[a-zA-Z0-9|a-zA-Z0-9\.]+\@[a-zA-Z0-9|a-zA-Z0-9\-]+\.[com|de|net|mu]+|Communication\s(\w+)")
    
    
            For Each Mt As Match In theRegex.Matches(s)
                ListBox1.Items.Add(Mt.Groups(0).Value)
                Debug.WriteLine(Mt.Groups(0).Value)
            Next
        End Sub
    the Debug Window
    Code:
    ...
    Communication detail
    customercare@abcdFtech.com
    admins@abcdFtech.com
    info@abcdFtech.com
    ...
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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