RETURN (REPLACE(REPLACE(REPLACE(@input, @class_text, '~~|~'), @id_text, @class_text), '~~|~', @id_text))
其中@input是字符串。
如何使用相同的参数在C#中转换它...
@class_text is class_text in C#, etc..
答案 0 :(得分:3)
input = input.Replace(class_text, "~~|~").Replace(id_text, class_text).Replace("~~|~", id_text);
简单就是那个
答案 1 :(得分:0)
T-SQL的REPLACE
实际上有不同的参数。例如,
REPLACE(string1, string2, replaceText)
在string1
内搜索string2
的位置,通常是:
string1.Replace(string2, replaceText)
...在.NET中,您在string2
中搜索string1
。对于大量的字符串替换,我建议使用StringBuilder
。
var builder = new Stringbuilder(input);
builder = builder.Replace(class_text, "~~|~").Replace(id_text, class_text).Replace("~~|~", id_text)
string result = builder.ToString();
答案 2 :(得分:0)
1)制作一个字符串
String myOldString = "No way";
2)使用替换功能
String myNewString = myOldString.Replace("No", "Yes");
以下是MSDN以获取更多详细信息:http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx#Y480
修改强> 以下是您帖子中的数据:
String myInput = "whatever @input equals"
String myclass_text = "whatever @class_text equals"
String myid_text = "whatever @id_text equals"
String myString = myInput.replace(myclass_text, "~~|~")
myString = myString.replace(myid_text, class_text)
myString = myString.replace("~~|~", myid_text)
或者一体机(我不知道c#是否可以让你像这样捎带,但如果更喜欢单行,请尝试一下:
String myString = myInput.replace(myclass_text, "~~|~").replace(myid_text, myclass_text).replace("~~|~", myid_text)
答案 3 :(得分:0)
将TSQL转换为C#,这可能是在原始问题How to change string element position in C#
中请求的 /// <summary>
/// Given a line of text, swap position of class and id tags.
/// Id should always precede class tag
///
/// </summary>
/// <param name="input"><td class="m92_t_col5" id="preis_0">xx</td></param>
/// <returns><td id="preis_0" class="m92_t_col5">xx</td></returns>
public static string SwapClassAndId(string input)
{
string output = string.Empty;
int classOrdinal = 0;
int classOridnalEndQuotes = 0;
string classText = string.Empty;
int idOrdinal = 0;
int idOrdinalEndQuotes = 0;
string idText = string.Empty;
classOrdinal = input.IndexOf("class=");
classOridnalEndQuotes = input.IndexOf("\"", classOrdinal + 7) + 1;
idOrdinal = input.IndexOf("id=");
idOrdinalEndQuotes = input.IndexOf("\"", idOrdinal + 4) + 1;
if (idOrdinal < classOrdinal)
{
return input;
}
classText = input.Substring(classOrdinal, classOridnalEndQuotes - classOrdinal);
idText = input.Substring(idOrdinal, idOrdinalEndQuotes - idOrdinal);
output = input.Replace(classText, "~~|~").Replace(idText, classText).Replace("~~|~", idText);
return output;
}