修剪字符串 - 删除VB.NET中的空格和括号

时间:2015-10-20 15:15:33

标签: vb.net string trim

有人会帮我这个吗?

在vb.net(VS2013)中: 字符串的格式为:char12345 (6789).jpg

将字符串修剪为:char12345.jpg

基本上,我需要修剪中间部分:空格和括号中的所有内容(包括括号)。

VB的修剪功能会起作用吗?或者我需要使用RegEx ...

非常感谢提前!

2 个答案:

答案 0 :(得分:2)

您不需要正则表达式,您也可以使用纯字符串方法删除parantheses:

Dim path = "char12345 (6789).jpg"
Dim ext = IO.Path.GetExtension(path)
Dim fn = IO.Path.GetFileNameWithoutExtension(path)
Dim index = fn.IndexOf("(")
If index >= 0 Then fn = fn.Remove(index).Trim()
path = String.Format("{0}{1}", fn, ext)

假设它们总是在扩展名之前,或者也可以删除它们后面的部分。否则它会变得更复杂:

Dim index = fn.IndexOf("(")
If index >= 0 Then
    Dim endindex = fn.LastIndexOf(")", index)
    If endindex >= 0 Then
        fn = fn.Remove(index).Trim() & fn.Substring(endindex + 1)
    Else
        fn = fn.Remove(index).Trim()
    End If
End If

答案 1 :(得分:2)

根据您的输入,您可以使用Split

完成此操作
Dim str as String = "char12345 (6789).jpg"
Console.Write(str.Split(" ")(0) & "." & str.Split(".")(1))
相关问题