Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用TRIM()删除尾部空白_Asp.net_Trim - Fatal编程技术网

Asp.net 使用TRIM()删除尾部空白

Asp.net 使用TRIM()删除尾部空白,asp.net,trim,Asp.net,Trim,我需要从没有前导空格或尾随空格的列表中获取项。我正在尝试以下代码,但Trim()函数仍然没有删除字符串的尾随空格。为什么会发生这种情况 string ab = string.Empty; ab += "first" + ", ";//adding a white space to the string ab += "second" + ", "; ab += "third" + ", "; Li

我需要从没有前导空格或尾随空格的列表中获取项。我正在尝试以下代码,但Trim()函数仍然没有删除字符串的尾随空格。为什么会发生这种情况

  string ab = string.Empty;
            ab += "first" + ", ";//adding a white space to the string
            ab += "second" + ", ";
            ab += "third" + ", ";

            List<string> ls = ab.ToString().Split(',').ToList();//first, second, third,

            foreach (string item in ls)
            {
                item.Trim();//need to remove the space
                string a = item;//here still got the white space
            }
string ab=string.Empty;
ab+=“第一个”+“,”//在字符串中添加空白
ab+=“第二”+“,”;
ab+=“第三”+“,”;
列表ls=ab.ToString().Split(',').ToList()//一,二,三,,
foreach(ls中的字符串项)
{
item.Trim();//需要删除空格
string a=item;//这里还有空白
}

Trim返回一个字符串,该字符串在开头和结尾都经过了空格字符的修剪,因此您需要将item.Trim()赋值给一个局部变量,该变量将成为修剪后的字符串

foreach (string item in ls)
{
    string trimmedItem = item.Trim(); //remove the space
    string a = trimmedItem;           //no white space here!
}
String.Trim()返回从当前System.String对象的开头和结尾删除所有空白字符后保留的字符串

因此,您需要将foreach循环中的代码更改为:

foreach (string item in ls) 
            { 
                string a = item.Trim();
            } 
如果按“,”而不是“,”分割,则无需修剪

foreach (string item in ls)
{
    string a = item.Trim();
}