\ char 的奇怪 TrimEnd 行为
我正在使用 TrimEnd 从字符串末尾删除某些字符。最初我认为这会起作用:
Dim strNew As String = "Employees\Sickness Entitlement.rpt"
Dim strTrim As String = "Sickness Entitlement.rpt"
Console.WriteLine(strNew.TrimEnd(strTrim)) '<- Doesn't work
但 TrimEnd 只适用于字符数组或单个字符字符串,所以我尝试了这个:
Dim strNew As String = "Employees\Sickness Entitlement.rpt"
Dim strTrim As String = "Sickness Entitlement.rpt"
Dim arrChars As Char()
ReDim arrChars(strTrim.Length)
For i As Integer = 0 To strTrim.Length - 1
arrChars(i) = strTrim.Substring(i, 1)
Next
Console.WriteLine(strNew.TrimEnd(arrChars)) '<- Employees\
这工作正常,直到我添加斜杠:
Dim strNew As String = "Employees\Sickness Entitlement.rpt"
Dim strTrim As String = "\Sickness Entitlement.rpt"
Dim arrChars As Char()
ReDim arrChars(strTrim.Length)
For i As Integer = 0 To strTrim.Length - 1
arrChars(i) = strTrim.Substring(i, 1)
Next
Console.WriteLine(strNew.TrimEnd(arrChars)) '<- Employ
现在输出:Employ
这是设计造成的吗?我觉得很奇怪。我的问题的解决方案是做类似的事情:
If strNew.EndsWith(strTrim) Then
Console.WriteLine(strNew.Substring(0, strNew.LastIndexOf(strTrim)))
End If
这既简单又有效,但是上面发生了什么?
I am using TrimEnd to remove certain characters from the end of a string. Initially I thought this would work:
Dim strNew As String = "Employees\Sickness Entitlement.rpt"
Dim strTrim As String = "Sickness Entitlement.rpt"
Console.WriteLine(strNew.TrimEnd(strTrim)) '<- Doesn't work
But TrimEnd only works for an array of chars or a single char string, so I tried this:
Dim strNew As String = "Employees\Sickness Entitlement.rpt"
Dim strTrim As String = "Sickness Entitlement.rpt"
Dim arrChars As Char()
ReDim arrChars(strTrim.Length)
For i As Integer = 0 To strTrim.Length - 1
arrChars(i) = strTrim.Substring(i, 1)
Next
Console.WriteLine(strNew.TrimEnd(arrChars)) '<- Employees\
This works fine until I add in the slash:
Dim strNew As String = "Employees\Sickness Entitlement.rpt"
Dim strTrim As String = "\Sickness Entitlement.rpt"
Dim arrChars As Char()
ReDim arrChars(strTrim.Length)
For i As Integer = 0 To strTrim.Length - 1
arrChars(i) = strTrim.Substring(i, 1)
Next
Console.WriteLine(strNew.TrimEnd(arrChars)) '<- Employ
This now outputs: Employ
Is this somehow by design? It seems strange to me. The solution to my problem is do do something like:
If strNew.EndsWith(strTrim) Then
Console.WriteLine(strNew.Substring(0, strNew.LastIndexOf(strTrim)))
End If
Which is both simpler and also works, but what is happening above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TrimEnd 正在删除您给它的所有字符,按照它找到它们的任何顺序,直到它找到不在列表中的字符。
因此,当 \ 不在您提供的列表中时,修剪将在 \ 处停止。包含 \ 后,修剪会删除 \,然后在字符串末尾看到“ess” - “e”和“s”都已在您提供的列表中,因此它们会被修剪。
Trim 方法完全不适合您想要做的事情。如果您要操作路径,请使用 Path.xxx 方法。如果您通常只是尝试将字符串分成多个部分,请使用 Split() 或 Substring() 的某种适当组合以及找到分割点所需的任何内容。
TrimEnd is removing all the characters you give it, in any order it finds them until it gets to a character that's not in the list.
So when the \ is not in the list you provide, the trimming stops at the \. Once you include the \, the trim removes the \ and then sees 'ess' on the end of the string - both 'e' and 's' are already in the list you provided, so they get trimmed.
The Trim methods are completely unsuitable for what you're trying to do. If you're manipulating paths, use the Path.xxx methods. If you're just generally trying to chop up strings into sections use either Split(), or some appropriate combination of Substring() and whatever you need to find the splitting point.
重新阅读
TrimEnd
< 的文档/a>;您使用错误:TrimEnd
将从字符串末尾删除数组中的任何 字符,只要它仍然找到此类字符。例如:
将输出
aa
,因为它删除了所有尾随的a
和b
。如果您的输入看起来与示例中完全相同,则最简单的递归是使用
Substring
:否则你可以求助于正则表达式。
Re-read the documentation of
TrimEnd
; you are using it wrong:TrimEnd
will remove any char that is in the array from the end of the string, as long as it still finds such chars.For example:
will output
aa
since it removes all trailinga
s andb
s.If your input looks exactly like in your example, your easiest recurse is to use
Substring
:Otherwise you can resort to regular expressions.