黑马程序员技术交流社区
标题:
关于字符串连接问题??
[打印本页]
作者:
孤独客
时间:
2013-6-25 12:09
标题:
关于字符串连接问题??
请教,比如:string[] time=new time[]{"abc","dea","fgh"};
想把这个字符串数组中3个字符串连接起来,如何做?
作者:
转达小朋友
时间:
2013-6-25 12:26
static void Main(string[] args)
{
//想把这个字符串数组中3个字符串连接起来,如何做?
//string[] time = new time[] { "abc", "dea", "fgh" };
string[] time = new string[] { "abc", "dea", "fgh" };
string str = "";
foreach (var item in time)
{
str += item;
}
Console.WriteLine(str);
Console.ReadKey();
}
复制代码
作者:
◇゛ 仅此而以
时间:
2013-6-25 12:29
这个可能没有直接的方法。只能遍历一下吧。
或是 放在一个List<String> 里
List<string> list = new List<string>();
string result = null ;
list.ForEach(delegate(string str)
{
result +=str;
});
复制代码
作者:
道法乾坤110
时间:
2013-6-25 17:14
定义一个空字符串,想在一个字符串里追加字符的话,建议用用StringBuilder
StringBuilder str = new StringBuilder();,具体网吧电脑没有VS软件,不能给你写具体代码,建议你去MSDN去输入StringBuilder,还有Append查一下,因为你这个问题不是什么复杂问题,相信你应该能看懂。
作者:
万大述
时间:
2013-6-25 18:02
由于在字符串参与的“+”运算符表示连接,所以只需用一个循环将个元素连接起来即可。参考代码如下:
string[] time = new string[] { "abc", "dea", "fgh" };
string str = "";
for(int i=0;i<time.Length;i++) //将数组元素取出并连接
{
str += time[i];
}
Console.WriteLine(str);
Console.ReadKey();
作者:
关关雎鸠
时间:
2013-6-26 18:45
遍历数组吧。。
StringBuilder sb = new StringBuilder();
for(int i = 0; i < time.length; i++){
sb.add(time[i]);
}
Console.WriteLine(sb.ToString());
复制代码
作者:
彭家贰小姐
时间:
2013-7-2 09:43
本帖最后由 彭家贰小姐 于 2013-7-2 12:48 编辑
不用循环也可以的:join一下
string[] time = new string[] { "abc", "dea", "fgh" };
Console.WriteLine( string.Join(",",time));
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2