问题描述
这是怎么回事?
issue.CallAction
具有以下值:
"Blah blah - WebSite - 9/20/2017 - Containers remaining changed to 0."
拆分它,就像这样:
issue.CallAction.Split("Containers remaining changed to ").ToList()
返回多个字符串元素(在这种情况下如预期),但是这些元素是:
returns more than 1 string element (as expected in this case) but the elements are:
-
Blah blah-WebSite-9/20/2017-
-
个保留的容器更改为0。
Blah blah - WebSite - 9/20/2017 -
ontainers remaining changed to 0.
据我了解, Split
会替换您所需要的整个字符串
It was my understanding that Split
replaces the entire string that you're splitting yet it's only replacing the first letter.
有什么用?
下面是编译器高兴的屏幕截图用字符串分隔符。请注意没有愤怒的红色花体:
Here's a screenshot of the compiler being happy with a string separator. Note the absence of the angry red squiggle:
:
推荐答案
似乎将分隔符视为 Char
。至少在我正在使用的Visual Studio 2017 / .NET 4.5,C#中,以及没有
It looks like it's treating the separator as a Char
. At least in Visual Studio 2017/.NET 4.5,C# which is what I'm using, as well as the current version there is no
string Split(string separator)
所以您必须创建一个分隔符数组并填充第一个一。
so you'd have to create an array of separators and populate the first one.
尝试一下:
string s = "Blah blah - WebSite - 9/20/2017 - Containers remaining changed to 0.";
string[] separator = { "Containers remaining changed to " };
string[] splitted = s.Split(separator, StringSplitOptions.None);
编辑:
我已经有好几年没有使用VB了,所以我不得不在这里重新学习。该示例程序的输出告诉我,当 String 强制为 Char
> Char 被调用:
I haven't used VB in several years, so I had to do some re-learning here. The output of this sample program tells me that VB quietly coerces a String
into a Char
when a Char
is called for:
Sub PrintAChar(c As Char)
Console.WriteLine("Printing your character: '" + c + "'")
End Sub
Sub Main()
PrintAChar("B") ' Prints 'B'
PrintAChar("Bob Was Here.") ' Also prints 'B'
End Sub
有关最佳做法,请参见下面的@Heinzi重要评论。
See @Heinzi's important comment below for a best practice.
这篇关于String.Split不删除拆分文本,仅删除第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!