String.Replace()不会更改变量的源字符串

时间:2017-01-16 18:04:23

标签: c#

我在c#应用程序中有以下代码: -

 string[] excludechar = { "|", "\\", "\"", "'", "/", "[", " ]", ":", "<", " >", "+", "=", ",", ";", "?", "*", " @" };
 var currentgroupname = curItemSiteName;
 for (int i = 0; i < excludechar.Length; i++)
       {
         if (currentgroupname.Contains(excludechar[i]))
             currentgroupname.Replace(excludechar[i], "");       
       }
 site.RootWeb.SiteGroups.Add(currentgroupname)

现在在我的abive代码中,我在currentgroupname函数内传递的.ADD变量将包含我在for循环中替换的所有特殊字符。如果我可以修改我的代码,那么任何人都可以这样做,以便.Replace实际上会替换currentgroupname的原始字符串......

2 个答案:

答案 0 :(得分:2)

您实际上并未将“已替换”字符串分配给with open ('myfile.txt', 'w') as filename: shutil.move(r'/curr/dir/myfile.txt', r'/new/dir/myfile.txt')

currentgroupname

答案 1 :(得分:0)

使用某些 $scope.processForm = function($scope.formData) { $http({ method : 'POST', url : 'process.php', data : $.param($scope.formData), // pass in data as strings headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) })

更容易
regex?

别忘了使用:

var input = "<>+--!I/have:many@invalid\\|\\characters";
var result = Regex.Replace(input, @"\W", "");
Console.Write(result); //Ihavemanyinvalidcharacters
相关问题