如何混合2个字符串C# [英] How can I mix 2 strings C#

查看:123
本文介绍了如何混合2个字符串C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  k =   ABC; 
string l = DEF;
string result = ADBECF;





 使用系统; 

命名空间 ADP_PG2_1_Solution
{
public class MixStringsExercise
{
public static string MixStrings( string s, string t){
// 示例:s = ABC,t = DEF ==> ADBECF
string L;
string next;
if (s.Length == t.Length){
L = s + t;
} 其他 {
return ;
}
for int i = 0 ; i < = s.Length; i ++){
return s =
}


return null < /跨度>;
}
}
}

解决方案

这个问题虽然没有意义但是,字符串结果看起来好像你想从每个中使用1个字符然后通过递增计数器并从两个字符数组(,其中,字符串)附加下一个字符来获得结果。



  var  builder =  new  StringBuilder(); 
string a = ABC,b = DEF;

for int i = 0 ; i < a.Length; i ++){
builder.Append(a [i]);
builder.Append(b [i]);
}

Console.WriteLine(builder.ToString());

// 输出:
// ADBECF





我用过的StringBuilder(正如谢尔盖在解决方案1的评论中所建议的那样)获得更好的性能,因为我不必担心这里的连接。然而,该算法仍然存在一些问题,因为它适用于相同长度的数组; 尝试使用索引较少的数组


您可以选择连接两个字符串:

  string  s1 =   ABC ; 
string s2 = DEF;

// 使用+运算符
string r1 = s1 + s2;

// 使用String.Concat
string r2 = String .Concat(s1,s2);

// 使用String.Format
string r3 = String .Format( {0} {1},s1,s2);

// 使用字符串文字(C#5)
< span class =code-keyword> string
r4 =


{S1} {S2};

// 使用StringBuilder(这个简单的任务有点矫枉过正)
string r5 = new StringBuilder(s1.Length + s2.Length).Append(s1 ).Append(s2).ToString();



你也可以在缓冲区中逐个复制字符。

  char  [] chars =  new   char (s1.Length + s2.Length); 
int index = 0 ;
for int i = 0 ; i < s1.Length; i ++){
chars [index ++] = s1 [i];
}
for int i = 0 ; i < s2.Length; i ++){
chars [index ++] = s2 [i];
}
string r6 = new 字符串(字符);





很抱歉我误读了你的问题。您可以通过稍微修改上面的代码块来实现它:

  char  [] chars =  new   char (s1.Length + s2.Length); 
int index = 0 ;
for int i = 0 ; i < s1.Length; i ++){
chars [index] = s1 [i];
index + = 2 ;
}
index = 1 ;
for int i = 0 ; i < s2.Length; i ++){
chars [index] = s2 [i];
index + = 2 ;
}
string output = new 字符串(字符);


string k = "ABC"; 
string l = "DEF"; 
string result = "ADBECF";



using System;

namespace ADP_PG2_1_Solution
{
	public class MixStringsExercise
	{
		public static string MixStrings(string s, string t) {
			//Example : s= ABC, t = DEF ==> ADBECF 
			string L ;
			string next ;
			if (s.Length == t.Length) {
				L = s + t;
					} else {
						return null;
						}
						for(int i = 0; i <= s.Length; i ++ ){
				return s = 
							}
				
		
			return null;
		}
	}
}

解决方案

The question although makes no sense, however, the string results seem as if you want to use 1 character from each and then get the result by incrementing the counter and appending the next characters from both arrays of characters (which, strings are).

var builder = new StringBuilder();
string a = "ABC", b = "DEF";

for (int i = 0; i < a.Length; i++) {
   builder.Append(a[i]);
   builder.Append(b[i]);
}

Console.WriteLine(builder.ToString());

// Output:
// ADBECF



I used StringBuilder (as Sergey had suggested in the comments to Solution 1) to get a better performance because I don't have to worry about the concatenation here. However, the algorithm still has some problems because it works on arrays of same length; try to use an array with less indices.


You have several choices to concat two strings:

string s1 = "ABC";
string s2 = "DEF";

// Using + operator
string r1 = s1 + s2;

// Using String.Concat
string r2 = String.Concat(s1, s2);

// Using String.Format
string r3 = String.Format("{0}{1}", s1, s2);

// Using string literal (C# 5)
string r4 =


"{s1}{s2}"; // Using a StringBuilder (a bit overkill for this trivial task) string r5 = new StringBuilder(s1.Length + s2.Length).Append(s1).Append(s2).ToString();


You can also copy the characters one by one in a buffer.

char[] chars = new char(s1.Length + s2.Length);
int index = 0;
for (int i = 0; i < s1.Length; i++) {
   chars[index++] = s1[i];
}
for (int i = 0; i < s2.Length; i++) {
   chars[index++] = s2[i];
}
string r6 = new String(chars);



[Edit] Sorry for I misread your question. You can achieve it bu only slightly modifying the code block above:

char[] chars = new char(s1.Length + s2.Length);
int index = 0;
for (int i = 0; i < s1.Length; i++) {
   chars[index] = s1[i];
   index += 2;
}
index = 1;
for (int i = 0; i < s2.Length; i++) {
   chars[index] = s2[i];
   index += 2;
}
string output = new String(chars);


这篇关于如何混合2个字符串C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆