DigitalDan.uk
VB.NET Handling Text
The functions outlined below are written in VB.net but it should be possible to modify them
for other computer languages.
This may need minor adjustments if you use special compiler options (Option Strict) because I have tried to simplify the code for ease of understanding
This may need minor adjustments if you use special compiler options (Option Strict) because I have tried to simplify the code for ease of understanding
' convert a line read from a CSV file to a list of fields Private Function readCSV(linein As String) As List(Of String) Dim ret As List(Of String) = New List(Of String) Dim c As Char = " " Dim temp As String = "" ret.Clear() If Not linein.Contains("""") Then ret.AddRange(linein.Split(",")) Return ret End If Dim quoted As Boolean = False linein &= "," ' ensures lat field is correctly processed For i As Integer = 1 To Len(linein) c = Mid(linein, i, 1) If ((Not quoted) And (c = ",")) Then ret.Add(temp) temp = "" Else temp &= c End If Next For i As Integer = 0 To ret.Count - 1 temp = ret.Item(i) temp = Trim(temp) ' remove leading and trailing quote If ((Mid(temp, 1, 1) = """") And (Mid(temp, Len(ret.Item(i)), 1) = """")) Then temp = Mid(temp, 2, Len(temp) - 1) temp = Mid(temp, 1, Len(temp) - 1) End If ' replace "" with " temp.Replace("""", "") ret.Item(i) = temp Next Return ret End Function ' convert a list of fields into a string that could be written to a csv file Private Function writeCSV(parts As List(Of String)) As String Dim ret As String = "" Dim temp As String For i As Integer = 0 To parts.Count - 1 temp = parts.Item(i) If temp.Contains("""") Then temp = temp.Replace("""", """""") End If If ((temp.Contains("""")) Or (temp.Contains(",")) Or (temp.Contains("'")) Or (temp <> Trim(temp))) Then temp = """" & temp & """" End If If i <> 0 Then ret &= "," ret &= temp Next Return ret End Function ' Remove any accents from a string Public Function RemoveAccents(txt As String) As String Dim normalizedString As String = txt.Normalize(System.Text.NormalizationForm.FormD) Dim ret As String = "" Dim c As Char Dim stringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder For i = 0 To normalizedString.Length - 1 c = normalizedString(i) If System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) <> System.Globalization.UnicodeCategory.NonSpacingMark Then stringBuilder.Append(c) End If Next Return stringBuilder.ToString() End Function ' Replacement for Left$( ... ) which was present inearly versions of Basic Public Function lleft(text As String, length As Integer) As String if length >= Len(text) Then Return text if length = 0 Then Return "" Return Mid(text, 1, length) End Function ' Replacement for Right$( ... ) which was present inearly versions of Basic Public Function rright(text As String, length As Integer) As String If length < 1 Then Return "" If length >= Len(text) Then Return text Return Mid(text, Len(text) - length + 1, length) End Function
DigitalDan.uk is part of the DigitalDan.co.uk group