c# - 如何在某些位置的字符串中插入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/640055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a value in a string at certain positions c#
提问by zohair
I have a program that gets a string from a method. I want to know how to insert a string value to that string at certain positions.
我有一个从方法中获取字符串的程序。我想知道如何在某些位置向该字符串插入字符串值。
For example:
例如:
mystring = "column1 in('a','b')column2 in('c','d')column3 in('e','f')";
Here, how would I insert the string value " and " after every occurance of the character ')' in mystring
?
在这里,我将如何在每次出现字符 ')' 后插入字符串值“和” mystring
?
PS. If possible, also include how not to insert it right at the end.
附注。如果可能,还包括如何不在最后插入它。
采纳答案by Quintin Robinson
You could accomplish this with replace..
你可以用替换来完成这个..
string mystring = "column1 in('a','b')column2 in('c','d')column3 in('e','f')";
mystring = mystring.Replace(")", ") and ").TrimEnd(" and".ToCharArray());
Resulting In:
导致:
"column1 in('a','b') and column2 in('c','d') and column3 in('e','f')"
回答by Frederik Gheysels
Strings are immutable, so you cannot 'just' change the value of that string. Each modification that you want to make to a string, leads to a new instance of a string.
字符串是不可变的,因此您不能“只是”更改该字符串的值。您想要对字符串进行的每次修改都会导致一个新的字符串实例。
This is maybe how you could achieve what you want:
这也许是你实现你想要的方式:
string s = " x in (a, b) y in (c, d) z in (e , f)";
string[] parts = s.Split (')');
StringBuilder result = new StringBuilder ();
foreach( string part in parts )
{
result.Append (part + ") and ");
}
Console.WriteLine (result.ToString ());
But maybe there are better solutions ...
但也许有更好的解决方案......
Anyway, how come you receive that string (which looks like a part of a where clause of a sql statement) in that way ?
无论如何,您怎么会以这种方式接收该字符串(它看起来像是 sql 语句的 where 子句的一部分)?
回答by Joel
Probably the simplest:
可能是最简单的:
mystring = mystring.Replace(")", ") and ");
mystring = mystring.Substring(0, mystring.Length - " and ".Length);
回答by Kev
If you mean, and I'm taking your string literally and as it comes:
如果你的意思是,我从字面上看你的字符串,因为它来了:
mystring = "column1 in('a','b')column2 in('c','d')column3 in('e','f')"
Then you could just do:
那么你可以这样做:
mystring = mystring.Replace(")c", ") and c");
Which would result in:
这将导致:
mystring =
"column1 in('a','b') and column2 in('c','d') and column3 in('e','f')"
This is presuming you don't want a trailing "and".
这是假设您不想要尾随的“和”。
回答by jdmichal
System.Text.RegularExpressions.Regex.Replace(
mystring, "\)(?=.+$)", ") and ");
The .+$
portion of the regular expression ensures that the closing parenthesis is not at the end of the line. If you are going to be doing this often, I'd recommend creating and persisting a Regex
object for the pattern.
.+$
正则表达式的一部分确保右括号不在行尾。如果您要经常这样做,我建议Regex
您为该模式创建并持久化一个对象。
// Do this once somewhere:
System.Text.RegularExpressions.Regex insertAndPattern =
new System.Text.RegularExpressions.Regex("\)(?=.+$)");
// And later:
insertAndPattern.Replace(mystring, ") and ");
EDIT: Just realized I'm an idiot. Fixed the patterns above from "\\).+$"
to "\\)(?=.+$)"
, so that the .+$
part is not included (and thereby replaced) in the match.
编辑:刚刚意识到我是个白痴。修复了上面从"\\).+$"
到的模式"\\)(?=.+$)"
,以便.+$
在匹配中不包含(并因此替换)该部分。