不知道我做错了什么,有问题的字符串是:
Type Family, Strategy
我将它存储在一个名为 item 的变量中,并调用 item.Trim() 但是输出没有变化。这是我整个函数的代码:
private bool checkFeatureList(string item, string feature, bool found)
{
//Only match if the feature is the first word TO DO
if (item.Contains(feature) && found == false)
{
int featureLength = feature.Length - 1;
item.Trim();
if (item.Substring(0, featureLength) == feature)
{
//Have not found the type yet, so add it to the array
found = true; //Only need the first match
//feature = item; //Split on double space TO DO
cleanFeatureList.Add(item);
}
}
return found;
}
我的目标是仅当第一个单词与“功能”匹配时才将“项目”添加到我的数组中。关于“featureLength”的一点只是为了得到第一个词,这不起作用,因为我的字符串在调用 item.Trim() 后仍然有前导空格。
在上面的例子中,我按照上面的指示传递了项目,“功能”是“类型”,“找到”是假的。
这是您当前的呼叫Trim
:
item.Trim();
该Trim
方法不会更改您正在调用的字符串的内容。它不能 - 字符串是不可变的。相反,它返回对应用了修剪的新字符串的引用。所以你要:
item = item.Trim();
请注意,您仍然需要额外的字符串操作来
适当处理,但这至少会根据需要从字符串的开头和结尾修剪空格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句