2017-01-16 9 views
-3

のソース文字列を変更しません: - 私のabiveコードで今String.Replace()私は私の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) 

私は.ADDの内側に渡していcurrentgroupname変数を関数は私のforループの中で置換したすべての特殊文字を持ちます。ですから、実際にcurrentgroupname

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

おそらくcurrentgroupname = currentgroupname.Replace(excludechar [i]、 ""); –

+0

'currentgroupname = currentgroupname.Replace(...);'いつものように、[documentation](https://msdn.microsoft.com/en-us/library/fk49wtc1(v = vs.110).aspx)をチェックしてください。 。 –

+2

文字列は不変です。新しい文字列を作成し、古い文字列を新しい文字列に置き換えます(^の後に) –

答えて

2

using System.Text.RegularExpressions; 
:使用を忘れてはいけないいくつかの regex?

var input = "<>+--!I/have:[email protected]\\|\\characters"; 
var result = Regex.Replace(input, @"\W", ""); 
Console.Write(result); //Ihavemanyinvalidcharacters 

とより簡単ではありません

0

に「置き換え」の文字列を代入されていません... .Replaceが実際にcurrentgroupnameの元の文字列を置き換えることになるので、私は私のコードを変更することができれば誰でもadivceでき

関連する問題