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 Thanks in advance Inge
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.
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(); }
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
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
For the main functions use String.Replace(" ", "") Then at the end of the string just use & vbCrLf e.g. Textbox1.Text = string & vbCrLf
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
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.
@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.
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()