Home > Software engineering >  how to remove a very spesific part from string in vb.net
how to remove a very spesific part from string in vb.net

Time:10-24

I have a string variable that should contains values like the folowing example:

Dim xStr as string = "13,14,133,15,2500,25"

I need to remove a very specific value from, like "13" but when I use replcae function I have many restrict to do that becasue when I will replace "13" with empty will be ",14,3,15,2500,25" so that's wrong becasue I just need to remove 13 and the comma after it if it was.

how can I apply that?

CodePudding user response:

Try this one,

Done with: Replace, TrimStart and TrimEnd methodes.

'A string sample
Dim xStr as string = "33, 13, , 14  ,133 ,15,  2500 ,25"

'Put a number to remove here
Dim number_to_remove=13

xStr = xStr.Replace(" ","") ' Removing any unnecessary whitespace
xStr="," xStr ","           ' Adding a padding comma

Console.WriteLine(xStr) 

' Removing wanted number
xStr = xStr.Replace(","   Cstr(number_to_remove)   ","  ,",")

' Removing any unnecessary comma        
if xStr.StartsWith(",") then   ' Remove coma At the beginning
   xStr = xStr.trimstart(",")
end if  
if xStr.EndsWith(",") then    ' Remove coma At the end
   xStr = xStr.trimend(",")
 end if 
xStr = xStr.Replace(",,",",") ' Remove coma in the middle

Console.WriteLine(xStr)     

[Input]

Note that the input is handled to be tolerant to spaces and empty element.

"33, 13, , 14  ,133 ,15,  2500 ,25"

In this case, the number to remove is 13.

[Output]

"33,14,133,15,2500,25"

CodePudding user response:

Try this:

Dim xStr as string = "13,14,133,15,2500,25"

xStr = xStr.Remove(0, xStr.IndexOf(",")   1).Trim()

If this answers your question please accept the answer.

CodePudding user response:

I would split the string into separate numbers (still strings) then the backwards For loop with Step -1 so we don't get an index out of range.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim xStr As String = "13,14,133,15,2500,25"
    Dim numToRemove As String = "13"
    Dim nums = xStr.Split(","c).ToList
    For index = nums.Count - 1 To 0 Step -1
        If nums(index) = numToRemove Then
            nums.RemoveAt(index)
        End If
    Next
    Dim NewString = String.Join(",", nums)
    MessageBox.Show(NewString)
End Sub

CodePudding user response:

Well, we have to make some assumptions here.

But, a extra (stray) spaces - yes, we should deal with that.

We also support if just ONE entry without comma's is the delete number.

So, this works quite well:

    Dim strToRemove As String = "14"

    Dim str = "13,14 ,133,15,2500,25,"

    str = str.Replace(" ", "")

    Dim strL As List(Of String) = str.Split(",").ToList
    strL.RemoveAll(Function(xRow) xRow = strToRemove)

    str = Join(strL.ToArray(), ",")

    Debug.Print("<" & str & ">")

outPut:

 <13,133,15,2500,25,>

The above also thus works with "empty" values for the separator.

And if we were in for punishment, we could probably write the whole deal with one line, but I would suggest that if you have frequent use of this code, then we go with this

Public Function RemoveToken(str as string, sRemove as string) as string

    str = str.Replace(" ", "")

    Dim strL As List(Of String) = str.Split(",").ToList
    strL.RemoveAll(Function(xRow) xRow = sRemove)

    return Join(strL.ToArray(), ",")

End Function
  • Related