C#在特定位置添加项的方式

huangapple go评论67阅读模式
英文:

C# way to add item in specific position

问题

我有一个包含如下数据的字符串数组,

string[] temp = new();
temp[0] = "abc";
temp[1] = "mno";
temp[2] = "xyz";

我需要的输出是,
我需要将值存储在特定的变量中

例如,

string value1 = "abc";
string value2 = "mno xyz";

因此,我希望在另一个字符串中组合索引1和2的数据。

英文:

I have string array which contains data like,

string[] temp = new();
temp[0] = "abc";
temp[1] = "mno";
temp[2] = "xyz";

I need the output like,
i need store value in specific variable

ex,

string value1 = "abc";
string value2 = "mno xyz"

so in another string i want the combine data of index 1 and 2.

答案1

得分: 2

尝试使用 String.Format();

在你的情况下,代码看起来像这样:

string value2 = String.Format("{0} {1}", temp[1], temp[2]);

// 另一种方法:
string value2 = $"{temp[1]} {temp[2]}";

你可以在这里阅读更多相关信息:String.Format() Microsoft Documentation


如果你想将数组的所有项连接成一个字符串,你可以使用:

string allElementsCombined = String.Join(' ', temp);

这将合并来自 temp 的所有字符串,并在它们之间添加空格字符。

你可以在这里阅读更多相关信息:String.Join() Microsoft Documentation

英文:

Try using String.Format();

In your case it would look like this:

string value2 = String.Format("{0} {1}", temp[1], temp[2]);

// Another way of doing this:
string value2 = $"{temp[1]} {temp[2]}";

You can read more about it here: String.Format() Microsoft Documentation


If you want to join all items of the array into the string you can use:

string allElementsCombined = String.Join(' ', temp);

This will combine all strings from temp and add space character between them.

You can read more about this here: String.Join() Microsoft Documentation

答案2

得分: 2

只需添加您想要的所有字符串

string sum = temp[1] + " " + temp[2];

> 要连接字符串变量,您可以使用 + 或 += 运算符,字符串插值或String.Format、String.Concat、String.Join或StringBuilder.Append方法。+ 运算符易于使用,使代码直观。即使在一个语句中使用多个 + 运算符,字符串内容也只会被复制一次。

https://learn.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings

英文:

Just add all string you want

string sum = temp[1] + " " + temp[2];

> To concatenate string variables, you can use the + or += operators, string interpolation or the String.Format, String.Concat, String.Join or StringBuilder.Append methods. The + operator is easy to use and makes for intuitive code. Even if you use several + operators in one statement, the string content is copied only once

https://learn.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings

答案3

得分: 2

要将数组值组合成字符串,只需执行以下操作:

string[] temp = new();
temp[0] = "abc";
temp[1] = "mno";
temp[2] = "xyz";

string value1 = temp[0];
string value2 = string.Format("{0} {1}", temp[1], temp[2]);
英文:

To combine array values into a string you can simply do the following:

string[] temp = new();
temp[0] = "abc";
temp[1] = "mno";
temp[2] = "xyz";

string value1 = temp[0];
string value2 = String.Format("{0} {1}",temp[1], temp[2]);

答案4

得分: 2

string[] temp = new string[] { "abc", "mno", "xyz" };
string value1 = temp[0];
string value2 = string.Join(" ", temp.Skip(1));
Console.WriteLine("value1 = " + value1);
Console.WriteLine("value2 = " + value2);
英文:
string[] temp = new string[] { "abc", "mno", "xyz" };
string value1 = temp[0];
string value2 = string.Join(" ", temp.Skip(1));
Console.WriteLine("value1 = " + value1);
Console.WriteLine("value2 = " + value2);

The Skip(1) method is used to skip the first element (i.e., "abc") and return the remaining elements as an IEnumerable<string>. Then, the string.Join() method is used to join the elements with a space separator, resulting in the string "mno xyz". Finally, the value1 and value2 variables are assigned the appropriate values, and their values are printed to the console.

huangapple
  • 本文由 发表于 2023年2月27日 15:35:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定