Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have array like
C#
string[] Phone = new string[25];


and i add data in phone array like this way

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


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

phone[0]="5465464 | 67895678";

for that what should i do ?
Posted
Comments
Shahin Khorshidnia 18-Jun-13 7:38am    
Phone[0] = string.Format("{0}|{1}", Phone[0], "67895678");
[no name] 18-Jun-13 7:56am    
Shahin Khorshidnia thank you it's also work.
Shahin Khorshidnia 18-Jun-13 8:05am    
Youre welcome

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

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


This is effectively the same as writing

C#
Phone[0] = Phone[0] + "|67895678"
 
Share this answer
 
v2
Comments
Rockstar_ 18-Jun-13 7:40am    
My 5!
Shahin Khorshidnia 18-Jun-13 7:43am    
var newNumber = "67895678";

newNumber=string.IsNullOrEmpty(Phone[0])?newNumber:"|"+newNumber;
Phone[0]=newNumber;
[no name] 18-Jun-13 7:56am    
also my +5
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
 
Share this answer
 
C#
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);

}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900