this is a

问题描述 投票:0回答:2

我有一个问题,修剪字符串的方法不完全工作,我已经审查MS文档和看论坛,但没有运气......。这可能是一些简单的东西或一些其他参数丢失。这只是一个例子,请注意,我需要拾取#前后的文本,因此比我计划使用#作为分隔符。Trim start @ #, Trim End @ #。我不能使用The last Index或Replace,根据我的理解,它们没有方向性。但也许我对MS文档中关于修剪开始和结束的内容有误解......谢谢!

 Dim str As String = "this is a #string"
        Dim ext As String = str.TrimEnd("#")
        MsgBox(ext)

答:我找到了解决我的问题的方法,如果你遇到类似的情况,请看下面的内容:1: Trim end不会像我原来想的那样从右边扫描 "字符",它只会从右边删除......。我想说,这是一个很弱的功能:)。) IndexOf方向ID将是一个非常简单和有用的。Regards 我的回答已经被Andrew回答了,谢谢!

现在有另一种方法,如果你尝试根据character分离来拆分一个single字符串into-qty,并相应地填充字段。答案是 阵列列表. 阵列列表将ID每个字符串,所以你可以避免重复的人口和等。后,你可以使用 CASE或IF 来相应地填充。

Dim arrList As New ArrayList("this is a # string".Split("#"c)) ' Will build the list of your strings
Dim index As Integer = 1 ' this will help us index the strings 1st, 2nd and etc.
For Each part In arrList 'here we are going thru the list
Select Case index ' Here we are identifying which field we are populating

       Case 1 '1st string(split)
         MsgBox("1 " & arrList(0) & index) '1st string value left to SPLIT arrList(0). 

       Case 2 '2nd string(split)
         MsgBox("2 " & arrList(1) & index) '2nd string value left to SPLIT arrList(1).  
 End Select

         index += 1 'Here we adding one shift thru strings as we go 

 Next
string vb.net trim
2个回答
0
投票

而不是。

Dim str As String = "this is a #string"
Dim ext As String = str.TrimEnd("#")

尝试:

Dim str As String = "this is a #string"
Dim ext As String = str.Replace("#", "")

0
投票
Dim str As String = "this is a #string"
Dim parts = str.Split("#"c)

For Each part in parts
    Console.WriteLine($"|{part}|")
Next

试试:输出。

© www.soinside.com 2019 - 2024. All rights reserved.