Hey Guys,

Any help on this would be greatly appreciated.

So I've got a list of strings in ListBox1 and I want to remove the last seven characters from each string in the list and write the output to an excel file...

Any tips would be greatly appreciated.

Thanks in advance

Dani AI

Generated

Quick summary and correction: wants each item in ListBox1 trimmed by removing the last seven characters and then saved to Excel. 's reply had a typo and addressed a single control only, while 's post took the first seven characters instead of removing the last seven. Below is a compact, robust pattern that handles non-string items, short items, and writes a single-column CSV that Excel can open.

' Requires: Imports System.IO, Imports System.Linq

Private Function TrimLastN(s As String, n As Integer) As String
    If String.IsNullOrEmpty(s) OrElse s.Length <= n Then
        Return s
    End If
    Return New String(s.Take(s.Length - n).ToArray())
End Function

' Build trimmed list from the ListBox and write a CSV
Dim trimmed = ListBox1.Items.Cast(Of Object)().
    Select(Function(o) TrimLastN(If(o Is Nothing, String.Empty, o.ToString()), 7)).
    ToList()

Dim csvLines = trimmed.Select(Function(x) """" & x.Replace("""", """""") & """")
File.WriteAllLines("C:\temp\output.csv", csvLines, System.Text.Encoding.UTF8)

Notes and troubleshooting

  • The helper returns the original item if it is shorter than or equal to seven characters to avoid exceptions.
  • The CSV is single-column and quotes values (double quotes inside values are escaped) so commas inside text are safe.
  • If Excel mis-parses decimals/dates or you need a real .xlsx, use a library such as EPPlus or ClosedXML to write native Excel files instead of CSV.
  • If ListBox items are custom objects, call the appropriate property (e.g., .ToString() or .MyField) before trimming.

Recommended Answers

All 3 Replies

Here is the code:

Dim result As String
result = beginingString.Text.Remove(beginingString.Text.Length - 7, 7)

Sorry mate, that reply didn't help too much at all (Beginning was spelt wrong and I don't even think that's a vb function anyway...)

I'm after code that will loop through each item in the listbox and remove the last 7 characters from each item and save the output to an excel file...

cheers guys :)

Dim tempList As New List(Of String) '// create temp list.
        For Each itm As String In ListBox1.Items '// loop thru listbox.
            If itm.Length > 7 Then '// to not get errors, check if item's length is greater than 7.
                tempList.Add(itm.Substring(0, 7)) '// if greater than 7, get the substring.
            Else
                tempList.Add(itm) '// if less than 7, add item as is.
            End If
        Next
        ListBox1.Items.Clear()
        ListBox1.DataSource = tempList '// add items back to the listbox.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.