String VB.net修剪功能

String VB.net修剪功能,string,vb.net,trim,String,Vb.net,Trim,我有一个修剪字符串的方法不完全工作的问题,我已经审查了MS文档和论坛的外观,但没有运气。。。可能是一些简单的东西,或者缺少其他参数。这只是一个样本, 请注意,我需要在#之前和之后提取文本,因此我计划使用#作为分隔符。修剪开始,修剪结束。根据我的理解,我不能使用上一个索引或替换,因为它们没有方向。但也许我误解了docs女士关于修剪起点和终点的看法。。。 谢谢 答复: 我找到了解决问题的方法,如果您遇到类似问题,请参见以下内容: 第一:修剪结束不会扫描的“字符”从右边,因为我原本以为它会删除它从右边

我有一个修剪字符串的方法不完全工作的问题,我已经审查了MS文档和论坛的外观,但没有运气。。。可能是一些简单的东西,或者缺少其他参数。这只是一个样本, 请注意,我需要在#之前和之后提取文本,因此我计划使用#作为分隔符。修剪开始,修剪结束。根据我的理解,我不能使用上一个索引或替换,因为它们没有方向。但也许我误解了docs女士关于修剪起点和终点的看法。。。 谢谢

答复: 我找到了解决问题的方法,如果您遇到类似问题,请参见以下内容: 第一:修剪结束不会扫描的“字符”从右边,因为我原本以为它会删除它从右边。。。。一个弱函数,我会说:)。方向ID的索引将是一个非常简单和有用的方法。我的回答是安德鲁回答的,谢谢

现在,如果您尝试根据字符分隔将单个字符串拆分为-QTY并相应地填充字段,则有另一种方法可以解决此问题。 答案是ArrayList。数组列表将标识每个字符串,以便在使用大小写或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
而不是:

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

输出:

|this is a | |string| |这是一个| |串|
也许有更好的方法,因为我们知道,做同一件事有多种方法。 我使用的解决方案如下:

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

这不是TrimEnd所做的。你需要LastIndexOf()+SubString(),这个字符串的末尾没有“#”。也许你可以解释一下你实际上想实现什么,而不是期望我们从没有实现的代码中计算出来。@jmchilinney我猜他想从字符串中删除散列字符。我们为什么要猜测呢?如果我们必须猜测,那么这不是一个有效的问题,所以应该关闭。请告诉你答案。不幸的是,我需要在之前和之后接。我想如果我能有一个。结束的工作,那么开始就是工作。我需要拾取这两个值,并根据“#”的位置将它们分开。我的计划是使用基于“#”的修剪开始修剪结束作为分隔符,但这在问题中没有定义。你只是想修剪问题中的散列字符。最初我想知道为什么修剪函数不起作用。但既然有人要我详细说明,我就是这么做的。但我还是想知道为什么修剪对我来说不起作用,谢谢你的回答,我以前试过。如果可能的话,我需要为换行符重新评估字符串后,会遇到这种复杂情况)我还不知道。用第一个填充表1,用第二个填充表2,因此我决定更容易修剪,谢谢你不知道你刚才说了什么,但不管是什么,你肯定没有在问题中提到它。在将来,我建议你花更多的时间思考这个问题,以及将来如何描述它,因为我们不应该在发布你实际提出的问题的答案后猜测和拖拽你的每一个细节。“修剪字符串方法不完全起作用”-这是一个原始问题。。。。我真的很欣赏周围的一切。。。。但现在的问题是为什么TrimEND不工作,除此之外,Andrew在上面澄清了这一点,因为我误解了函数的工作原理。我还根据请求更新了原始问题。现在,如果我可以得到一个帮助,我可以得到它是伟大的,谢谢!意思是如何将|这是一个|到1个表,将|字符串|到第2个表,thanks@Alek
parts
在这个答案中是一个数组。如果您想访问它的项目,请使用
part(0)
part(1)
Dim str As String = "this is a #string"
Dim parts = str.Split("#"c)

For Each part in parts
    Console.WriteLine($"|{part}|")
Next
|this is a | |string|
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