Need help with a small VB Script

Discussion in 'Mixed Languages' started by Inge, Nov 11, 2014.

  1. Inge

    Inge MDL Member

    Apr 2, 2008
    185
    465
    10
    #1 Inge, Nov 11, 2014
    Last edited by a moderator: Apr 20, 2017
    Hi there. I am new in VB.NET and need a solution to solve the following problem.

    There is an address in this format:

    Code:
    Line1
    <CRLF>Line2
    <CRLF>Line3
    <CRLF>Line4
    <CRLF>Line5

    I need to re-format this address and double the last vbcrlf:

    Code:
    Line1
    <CRLF>Line2
    <CRLF>Line3
    <CRLF>Line4
    <CRLF>
    <CRLF>Line5

    :worthy: Thanks in advance :yes:



    Inge
     
  2. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,473
    1,292
    60
    #2 QuantumBug, Mar 28, 2015
    Last edited by a moderator: Apr 20, 2017
    I seriously think a more, in depth? explanation is needed here bud.

    Do you just need to double break?

    Use "Public eNl As Object = Environment.NewLine" or "Dim eNl as Object = Environment.NewLine (needs to be added in any new code block)

    Then use my reference below:

    Code:
    Messagebox.Show("Break here twice" & eNl & eNl & "To breaks above")
    Also you can just double vbCrLf:

    Code:
     "" & vbCrLf & vbCrLf & ""
    The AND "&" operator can be used to combine the two breaks.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Inge

    Inge MDL Member

    Apr 2, 2008
    185
    465
    10
    #3 Inge, Mar 28, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thanks Dev.

    The address fields vary in length. Some has 3 lines, others 4, 5 or 6.


    I need something like this loop in VB:

    Code:
    static void Main(string[] args)
    {
    string result = "", s = "This\r\nIs\r\nMy\r\nAddress";
    string[] array = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
    
    Console.WriteLine("Unmodified:\r\n--------------------\r\n" + s);
    
    for(int i = 0; i<array.Length; i++)
    {
    result += System.String.IsNullOrWhiteSpace(result) ? array : ((i < array.Length - 1 ? "\r\n" : "\r\n\r\n") + array);
    }
    
    Console.WriteLine();
    Console.WriteLine("Modified:\r\n--------------------\r\n" + result);
    
    Console.ReadKey();
    }
    
     
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,473
    1,292
    60
    #4 QuantumBug, Mar 30, 2015
    Last edited by a moderator: Apr 20, 2017
    Are you wanting to remove whitespace or add? You're still not explaining clearly so I converted the code for you.

    Code:
    Private Shared Sub Main(args As String())
        Dim result As String = "", s As String = "This" & vbCr & vbLf & "Is" & vbCr & vbLf & "My" & vbCr & vbLf & "Address"
        Dim array As String() = s.Split(New Char() {ControlChars.Cr, ControlChars.Lf}, StringSplitOptions.RemoveEmptyEntries)
    
    
        Console.WriteLine(Convert.ToString("Unmodified:" & vbCr & vbLf & "--------------------" & vbCr & vbLf) & s)
    
    
        For i As Integer = 0 To array.Length - 1
            result += If(System.[String].IsNullOrWhiteSpace(result), array(i), ((If(i < array.Length - 1, vbCr & vbLf, vbCr & vbLf & vbCr & vbLf)) + array(i)))
        Next
    
    
        Console.WriteLine()
        Console.WriteLine(Convert.ToString("Modified:" & vbCr & vbLf & "--------------------" & vbCr & vbLf) & result)
    
    
        Console.ReadKey()
    End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Inge

    Inge MDL Member

    Apr 2, 2008
    185
    465
    10
    #5 Inge, Mar 30, 2015
    Last edited: Mar 30, 2015
    (OP)
    I'ld like to remove all blank lines and add one before the very last line.

    Examples:
    =======================
    Input:
    > Line 1
    > Line 2
    > Line 3
    ------------------------------
    Result:
    > Line 1
    > Line 2
    > CRLF
    > Last line
    =======================
    Input:
    > Line 1
    > Line 2
    > Line 3
    > CRLF
    > Line 5
    > Line 6
    ------------------------------
    Result:
    > Line 1
    > Line 2
    > Line 3
    > Line 4
    > CRLF
    > Last line



    Edit: In C# it's easy for me and I've just found some online converters (CS to VB) which works fine.


    Thanks for your support ;)
     
  6. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,473
    1,292
    60
    #6 QuantumBug, Mar 30, 2015
    Last edited: Mar 30, 2015
    For the main functions use String.Replace(" ", "")

    Then at the end of the string just use & vbCrLf e.g. Textbox1.Text = string & vbCrLf
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Inge

    Inge MDL Member

    Apr 2, 2008
    185
    465
    10
    #7 Inge, Mar 30, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I really hate VB, but this few lines work as expected:

    Code:
    Private Function UpdateAddress(address As String) As String
    Dim result As String = String.Empty
    If address IsNot Nothing Then
    Dim lines As String() = address.Split(New Char() {ControlChars.Cr, ControlChars.Lf}, System.StringSplitOptions.RemoveEmptyEntries)
    For i As Integer = 0 To lines.Length - 1
    If Not String.IsNullOrWhiteSpace(lines(i).Trim()) Then
    result += If(String.IsNullOrWhiteSpace(result), lines(i).Trim(), ((If(i < lines.Length - 1, vbCrLf, vbCrLf & vbCrLf)) + lines(i).Trim()))
    End If
    Next
    End If
    Return result
    End Function
    
     
  8. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,473
    1,292
    60
    You want to work as a software developer? Don't hate VB, embrace it. You'll go farther than you could ever imagine.

    Great work finding out how to solve your issue though. You should mark the thread as solved.

    Regards, The Dev.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    418
    60
    @The Dev

    You made this more complicated than needed!

    To do this you can split the input by the vbCRLF and just insert a line into the array - As he did above.. Instead of doing all the replace and what not.

    To your .Replace(" ", "") - .Trim() does what he asks - It removes leading and trailing whitespace from a string.
     
  10. Xadiaris

    Xadiaris MDL Novice

    Apr 23, 2008
    27
    9
    0
    #10 Xadiaris, Apr 2, 2015
    Last edited by a moderator: Apr 20, 2017
    Here you are
    Code:
            Dim result As String = "", s As String = "This" & vbCrLf & "Is" & vbCrLf & "My" & vbCrLf & "Address"
            Dim array As List(Of String) = s.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries).ToList
    
    
            Console.WriteLine(Convert.ToString("Unmodified:" & vbCrLf & "--------------------" & vbCrLf) & s)
    
            array.Insert(array.Count - 1, vbNullString)
            result = String.Join(vbCrLf, array)
    
            Console.WriteLine()
            Console.WriteLine(Convert.ToString("Modified:" & vbCrLf & "--------------------" & vbCrLf) & result)
    
    
            Console.ReadKey()
     
  11. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    418
    60
    #11 PAYMYRENT, Apr 4, 2015
    Last edited: Apr 4, 2015
    +1

    Thanks! Too busy with Reboot-To and crap WCF lol