将字符串连接到 C# 中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1149380/
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
concatenate string to char in C#
提问by jarus
I need to concatenate '*', which is a string, to a character in an array.
我需要将字符串 '*' 连接到数组中的一个字符。
For example:
例如:
int count=5;
string asterisk="*";
char p[0]='a';
char p[1]='b';
char p[2]='a';
char p[3]='b';
char p[4]='b';
for(int i=0;i<count;i++)
{
asterisk=asterisk+"*";
}
p[0]=p[0]+asterisk;
How can I do this? I want the result to be like "a*****
"
我怎样才能做到这一点?我希望结果像“ a*****
”
回答by Mark Carpenter
Your example doesn't look like valid c# to me. If all you're trying to do is concatenate a bunch of asterisks at the end of a string, this should work:
您的示例对我来说看起来不像有效的 c#。如果您要做的只是在字符串的末尾连接一堆星号,那么这应该有效:
string myString = "a";
for(int x = 0; x < 5; x++){
myString += "*";
}
//myString should now equal "a*****"
回答by CMS
You are trying to store the resulting strings in the same char
array, and that is not possible because in one char variable you can only store one char, you should use a string[]
or a List<string>
to store the result...
您试图将结果字符串存储在同一个char
数组中,这是不可能的,因为在一个 char 变量中您只能存储一个 char,您应该使用 astring[]
或 aList<string>
来存储结果......
List<string> result = new List<string>();
string asterisk = new string('*', 5); // Get a string with five *
foreach (char c in charArray)
{
result.Add(c + asterisk);
}
Or if you have access to Linq to Objects:
或者,如果您有权访问 Linq to Objects:
var result = charArray.Select(c => c + asterisk); // Select all
// chars and concatenate
// the variable
回答by Senthil Kumar
You cannot concatenate a string to a character. A string is a collection of characters, and won't "fit" inside a single character.
不能将字符串连接到字符。字符串是字符的集合,不会“适合”在单个字符中。
You probably want something like
你可能想要类似的东西
char asterisk = '*';
string []p = new string[] { "a", "b", "a", "b" };
p[0] = p[0] + new string(asterisk, count);
回答by Kobi
Generally, this should be done using a StringBuilder
, that whould give better performances (depending on your code and how many time you run it).
Also, String has a constructor that takes a char and a number, and gives that char n times in a string: http://msdn.microsoft.com/en-us/library/aa331867%28VS.71%29.aspx
Third, have a look at String.ToCharArray
, as in
通常,这应该使用 a 来完成,这样可以StringBuilder
提供更好的性能(取决于您的代码以及运行它的次数)。
此外,String 有一个构造函数,它接受一个字符和一个数字,并在一个字符串中给出该字符 n 次:http: //msdn.microsoft.com/en-us/library/aa331867%28VS.71%29.aspx
Third ,看看String.ToCharArray
,如
char[] chars = "abcd".ToCharArray();
This can save you some lines there.
这可以为您节省一些行。
回答by Bill Crim
I think the problem is the word "concatenate". I think he wants to overwrite. So he can show a semi-password like string...
我认为问题在于“连接”这个词。我认为他想覆盖。所以他可以显示像字符串一样的半密码......
char[] p = { 'a', 'b', 'a', 'b', 'b' };
char[] asterisks = (new String('*', p.Length -1)).ToCharArray();
asterisks.CopyTo(p, 1);
.CopyTo() will write the "asterisks" array to the "p" array. Previous posters are right in that you should use StringBuilder for manipulation like this, but if you HAVE to have it as an array of chars, this is the way to do it. (Assuming that I understand what you want."I want the result to be like "a*****". ")
.CopyTo() 将“星号”数组写入“p”数组。以前的海报是正确的,您应该使用 StringBuilder 进行这样的操作,但是如果您必须将其作为字符数组使用,这就是这样做的方法。(假设我明白你想要什么。“我希望结果像“a*****”。”)