字符串数组与它在c#中的旧值连接 [英] string array concat with it's old value in c#

查看:61
本文介绍了字符串数组与它在c#中的旧值连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组类似

i have array like

string[] Phone = new string[25];





我在这样的手机阵列中添加数据





and i add data in phone array like this way

Phone[0]="5465464";
Phone[1]="5465464";
Phone[2]="5465464";
Phone[3]="5465464";
Phone[4]="5465464";
Phone[5]="5465464";





现在想在电话[0] =5465464中附加67895678;

所以我想要



手机[0] =5465464 | 67895678;



那是什么我应该这样做吗?



now want to append 67895678 in Phone[0]="5465464";
so i want

phone[0]="5465464 | 67895678";

for that what should i do ?

推荐答案

很容易。 + = 运算符将允许您将值附加到字符串或对数字值应用添加。



Nice an easy. The += operator will allow you to append a value to a string or apply an addition to a number value.

Phone[0] += "|67895678"





这与写作实际上相同





This is effectively the same as writing

Phone[0] = Phone[0] + "|67895678"

即使您可以附加字符串,如果您需要重复执行此操作,也不建议这样做,因为此类操作的性能会很差。字符串是不可变的。附加字符串时,始终会创建一个全新的实例。我是否甚至需要解释这对性能的影响?



所以,如果你有几个小的固定数量的字符串串联,使用的String.format 。如果组合字符串的方案更复杂,例如使用循环或其他构造(ifs等),请使用可变类型 System.Text.StringBuilder

http://msdn.microsoft.com/en-us/ library / system.text.stringbuilder.aspx [ ^ ]。



-SA
Even though you can append strings, this is not recommended if you need to do it repetitively, as such operation will have poor performance. The strings are immutable. When you append strings, a brand-new instance is always created. Do I even need to explain the implication of this on performance?

So, if you have several concatenation of small fixed number of strings, use string.Format. If you scheme of composing a string is more complex, such with loops or other constructs (ifs, and so on), use the mutable type System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

—SA


using System;
public class BaseClass
{
static void Main()
{
string[] Phone = new string[5];
Phone[0]="5465464";
Phone[1]="5465464";
Phone[2]="5465464";
Phone[3]="5465464";
Phone[4]="5465464";
foreach(string p in Phone)
{
    Console.WriteLine("Phone{}"+p);

}
string a=Phone[0]+"67895678";
Console.WriteLine(a);

}

}


这篇关于字符串数组与它在c#中的旧值连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆