2017-05-31 13 views
-5

文字列「DT1OutPassFail」を「DT4OutPassFail」に変更する必要があります。私は、入力文字列で "DT1"を見つけ、 "DT4"で置き換えて出力文字列を取得する必要があります。私はC#を使用してこれを行う必要があります。 "DT1"はtextbox1の値で、 "DT4"はtextbox2の値です。私は次のオプションを試しました。しかし、それは仕事量です。c#を使用して単語の一部を置換する

string input = "DT1OutPassFail"; 
string newstring; 

newstring = input.Replace(textbox1.Text, textbox2.Text); 

newstring = Regex.Replace(input,textbox1.Text, textbox2.Text); 

答えて

0
public string replaceString(string value) 
{ 
    string newValue; 
    string findValue; 
    string replaceValue; 

    findValue = textBox1.Text; 
    replaceValue = textBox2.Text; 

    if(value.StartsWith(findValue)) 
     newValue = value.Replace(findValue, replaceValue); 
    else 
     newValue = value; 

    return newValue; 
} 
0

代わりにこの方法を試してみてください:

//The Original string 
string input = "DT1OutPassFail"; 
//The string which you want to replace with DT1 
string input2="DT4"; 
//Just check whether the string contains DT1 then replace it with input2 
var result = input.Contains("DT1") ? input.Replace("DT1", input2) : input; 
関連する問題