从字符数组获取字符串 [英] Getting String from Char Array
问题描述
你好,
在程序中,我需要操纵输入字符串.
简单地说,我应该在字符串的每个两个字母之间加上一个逗号,".
我使用了下面的代码,但是在修改了字符串后,
修改后的字符串显示不正确.换句话说,假设
输入字符串:"Hello"
修改后的字符串:``H,e,l,l,o''
但显示:System.String []
Hello,
In a program I need to manipulate the input string.
Simply I should put a comma '','' between each two letters of the string.
I used the code below but after modifying the string,
the modified string is not shown well. In other words, suppose
input string : ''Hello''
modified string : ''H,e,l,l,o''
but shown : System.String[]
string inp_str, mod_str; // Input string , modified string
inp_str = textBox1.Text;
int intLng = inp_str.Length;
char[] chAray = new char[2*intLng];
for (int i1 = 0; i1 < inp_str.Length; i1++)
{
chAray[2 * i1] = inp_str[i1];
chAray[2 * i1 + 1] = '','';
}
mod_str = chAray.ToString();
MessageBox.Show(mod_str);
你能帮我吗?
非常感谢
Could you please help me?
Thanks a lot
推荐答案
一种简单的方法是使用
mod_str = new string(chAray);
新的字符串构造函数是一种从数组中创建字符串的快速方法.
The new string constructor is a fast method for creating the string out of an array.
string inp_str, mod_str;
inp_str = textBox1.Text;
mod_str = string.Join(",", inp_str.ToCharArray());
inp_str = textBox1.Text;
替代3是很好的选择.但是,即使考虑使用LINQ创建的字符串连接CSV/PSV字符串 [ ^ ]
小心
Alternative 3 is the good one. However consider even this String concatenation using LINQ to create a CSV/PSV string[^]
Take care
这篇关于从字符数组获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!