将两个字符串变量添加到双字符串数组中 [英] Add two string variables to a double string array

查看:85
本文介绍了将两个字符串变量添加到双字符串数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在双字符串数组中添加两个字符串变量:



I would like to add two string variables to a double string array:

string a = "foo1";
string b = "foo2";
string[] c = new string[10];
c = ?????(a,b);





然后如何将双字符串数组分成两个单字符串变量:





Then how do you separate a double string array to two single string variables:

d = ???(c);
e = ???(c);

推荐答案

最简单的方法:选择一个不能在eitehr字符串中的字符:

Easiest way: pick a character that can't be in eitehr string:
string a = "foo1";
string b = "foo2";
string combined = string.Join("|", a, b);



要再次分开它们,只需使用Split:


To separate them again, just use Split:

string[] parts = combined.Split('|');
string a = parts[0];
string b = parts[1];



但是如果你这样做是为了将信息存储在一个文件或数据库中,那么你可以做的事情可能更好,而不是乱用字符串连接!


But if you are doing this to store the information in a file or database, there are probably better things you can do instead of messing with string concatenation at all!


它目前尚不清楚你想要达到的目标,但是有些样本...

It is not clear what exactly do you want to achieve, but some samples...
string[] arr = new string[] {"stringA", "stringB"};
string A = arr[0];
string B = arr[1];
string[] arr1 = new string[] {A, B};


这篇关于将两个字符串变量添加到双字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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