本文介绍了c尖锐的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个字符串Rahul,Mishra我想显示像
frist字母的第一个字母然后第一个字符串的第一个字母... 。
前RAHUL
MISHRA
输出:
RMAIHSUHLRA
i have two strings "Rahul" ,"Mishra" i want to show like
frist letter of frist string then first letter of second string ...then 2 letter of first string ....
ex-RAHUL
MISHRA
output:
RMAIHSUHLRA
推荐答案
using System.Linq;
static void Main(string[] args)
{
string mixed = Interleave("RAHUL", "MISHRA");
}
public static string Interleave(string s1, string s2)
{
StringBuilder sb = new StringBuilder();
s1.Zip(s2, (c1, c2) => sb.Append(c1).Append(c2)).Count(); // .Count() to force the Lazy evaluation!
if (s1.Length > s2.Length)
{
sb.Append(s1.Substring(s2.Length));
}
else if (s2.Length > s1.Length)
{
sb.Append(s2.Substring(s1.Length));
}
return sb.ToString();
}
public static string GetString(string string1,string string2)
{
char[] str1 = string1.ToCharArray();
char[] str2 = string2.ToCharArray();
string finalString = "";
int length = str1.Length > str2.Length ? str2.Length : str1.Length;
for (int i = 0; i < length ; i++)
{
finalString = finalString + str1[i] + str2[i];
}
if (str1.Length > str2.Length)
{
int difference = str1.Length - str2.Length;
for (int i = 0; i < difference; i++)
{
char rem = str1[str2.Length + i];
finalString = finalString + rem;
}
}
else
{
int difference = str2.Length - str1.Length;
for (int i = 0; i < difference; i++)
{
char rem = str2[str1.Length + i];
finalString = finalString + rem;
}
}
return finalString;
}
如下所示传递你的字符串
Pass your string like below
string finalString = GetString("RAHUL", "MISHRA");
希望这有帮助
Hope this helps
这篇关于c尖锐的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!