C# 根据字符索引合并两个字符串
我正在尝试编写一个代码,根据字符索引合并两个字符串。例如,如果我们有两个字符串“abc”和“defg”,我需要一个字符串output1(合并两个字符串的所有偶数字符)=“adcf”和另一个字符串output2=“beg”(剩余所有单词) 我尝试过的-C# 根据字符索引合并两个字符串,c#,string,C#,String,我正在尝试编写一个代码,根据字符索引合并两个字符串。例如,如果我们有两个字符串“abc”和“defg”,我需要一个字符串output1(合并两个字符串的所有偶数字符)=“adcf”和另一个字符串output2=“beg”(剩余所有单词) 我尝试过的- class Program { static void Main(string[] args) { string a= "First"; strin
class Program
{
static void Main(string[] args)
{
string a= "First";
string b= "MiddleName";
string newstring = "";
string newstring1 = "";
int length = b.Length;
for (int l = 0; l < length; l=l+1)
{
if(l%2==0)
{
newstring = newstring + a[l].ToString() + b[l].ToString();
}
if (l % 2 == 1)
{
newstring1 = newstring1 + a[l].ToString() + b[l].ToString();
}
}
Console.ReadLine();
}
}
类程序
{
静态void Main(字符串[]参数)
{
字符串a=“第一”;
字符串b=“中间名”;
字符串newstring=“”;
字符串newstring1=“”;
int长度=b.长度;
对于(int l=0;l
但在这种情况下,它会给出绑定数组之外的异常。有更好的方法吗
谢谢我建议提取一种方法,在该方法中,您应该解决合并两个字符串的一般问题,即从偏移量开始,从每个步骤中提取字符:
private static String Merge(String left, String right, int step, int offset) {
StringBuilder sb = new StringBuilder();
if (null == left)
left = ""; // or throw exception
if (null == right)
right = ""; // or throw exception
for (int i = offset; i < Math.Max(left.Length, right.Length); i += step) {
//DONE: do not forget to check if you can get a character
if (i < left.Length)
sb.Append(left[i]);
//DONE: do not forget to check if you can get a character
if (i < right.Length)
sb.Append(right[i]);
}
return sb.ToString();
}
这是因为B的单词比A长。所以当它的迭代大于A'的长度时,它会导致错误。
所以在添加之前,您需要检查A是否有那么多单词
如果B'长度始终大于A,则可以使用下面的代码
class Program
{
static void Main(string[] args)
{
string a= "First";
string b= "MiddleName";
string newstring = "";
string newstring1 = "";
int length = b.Length;
for (int l = 0; l < length; l=l+1)
{
if(l%2==0)
{
if(a.Length > l)
{newstring += a[l].ToString();}
newstring += b[l].ToString();
}
if (l % 2 == 1)
{
if(a.Length > l)
{newstring1 += a[l].ToString();}
newstring1 += b[l].ToString();
}
}
Console.ReadLine();
}
}
类程序
{
静态void Main(字符串[]参数)
{
字符串a=“第一”;
字符串b=“中间名”;
字符串newstring=“”;
字符串newstring1=“”;
int长度=b.长度;
对于(int l=0;ll)
{newstring+=a[l].ToString();}
newstring+=b[l].ToString();
}
如果(l%2==1)
{
如果(a.长度>l)
{newstring1+=a[l].ToString();}
newstring1+=b[l].ToString();
}
}
Console.ReadLine();
}
}
用于(int l=0;l
您的长度
必须是较短的字符串长度,循环后,将较长字符串中的所有剩余字符追加到newstring1
。但这样就不会占用长字符串的所有偶数字符(int l=0;l
@mohsen这将做与我相同的事情,其他长字符串的字符将丢失。@vic90您必须在循环后将长字符串中的所有剩余字符追加到newstring1
。非常感谢,先生。好主意
class Program
{
static void Main(string[] args)
{
string a= "First";
string b= "MiddleName";
string newstring = "";
string newstring1 = "";
int length = b.Length;
for (int l = 0; l < length; l=l+1)
{
if(l%2==0)
{
if(a.Length > l)
{newstring += a[l].ToString();}
newstring += b[l].ToString();
}
if (l % 2 == 1)
{
if(a.Length > l)
{newstring1 += a[l].ToString();}
newstring1 += b[l].ToString();
}
}
Console.ReadLine();
}
}
for (int l = 0; l < b.length && l < a.length; l++)
{
if(l%2==0)
{
newstring += a[l]+ b[l];
}
if (l % 2 == 1)
{
newstring1 += a[l] + b[l];
}
}