Strting.Trim() 未使用 C# 删除我的网络抓取工具中的前导和尾随空格
Strting.Trim() is not removing leading and trailing spaces in in my web scraper, using C#
不确定我做错了什么,有问题的字符串是:
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;
}
我的目标是仅当第一个单词匹配 "feature" 时才将 "item" 添加到我的数组中。关于 "featureLength" 的一点只是为了获取第一个单词,这是行不通的,因为在调用 item.Trim().
之后我的字符串仍然有前导空格
在上面的示例中,我按照上面的指示传入项目,"feature" 是 "Type" 并且 "found" 是假的。
这是您当前对 Trim
的呼叫:
item.Trim();
Trim
方法不会更改您正在调用的字符串的内容。它不能——字符串是不可变的。相反,它 returns 引用了 new 字符串并应用了 trimming。所以你想要:
item = item.Trim();
请注意,您仍然需要额外的字符串操作才能正确处理
,但这至少会根据需要从字符串的开头和结尾留出 trim 个空格。
不确定我做错了什么,有问题的字符串是:
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;
}
我的目标是仅当第一个单词匹配 "feature" 时才将 "item" 添加到我的数组中。关于 "featureLength" 的一点只是为了获取第一个单词,这是行不通的,因为在调用 item.Trim().
之后我的字符串仍然有前导空格在上面的示例中,我按照上面的指示传入项目,"feature" 是 "Type" 并且 "found" 是假的。
这是您当前对 Trim
的呼叫:
item.Trim();
Trim
方法不会更改您正在调用的字符串的内容。它不能——字符串是不可变的。相反,它 returns 引用了 new 字符串并应用了 trimming。所以你想要:
item = item.Trim();
请注意,您仍然需要额外的字符串操作才能正确处理
,但这至少会根据需要从字符串的开头和结尾留出 trim 个空格。