如何使用vb.net删除字符串中的字符?

Anu*_*uya 1 .net vb.net asp.net

string Example 1 : abc / 
string Example 2 : abc / cdf / / 
string Example 3 : abc / / / /
string Example 4 : / / / / / 
string Example 5 : abc / / xyz / / /  
Run Code Online (Sandbox Code Playgroud)

我需要在很多场景中删除字符串中的斜杠.我猜这些情景在下面的预期结果中是自我解释的.

结果:

    string Example 1 : abc 
    string Example 2 : abc / cdf 
    string Example 3 : abc  
    string Example 4 :  
    string Example 5 : abc / xyz 
Run Code Online (Sandbox Code Playgroud)

如何使用vng.net这样做?

Sys*_*gon 6

试试这个:

Dim s As String '= ...
Dim aux() As String

aux = s.Split(New Char() {"/"c}, StringSplitOptions.RemoveEmptyEntries)
s = String.Join("/", aux)
Run Code Online (Sandbox Code Playgroud)

您可能想要处理空白区域:

aux = s.Split("/ ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
s = String.Join(" / ", aux)
Run Code Online (Sandbox Code Playgroud)

  • 这不会导致带有额外空白区域的字符串吗?可能需要`.Trim()`数组中的字符串才能加入它们. (2认同)