我有一个字符串,需要该字符串中的字母。
string s1 = "SpendSS-S";
string s2 = "Spendss-M";
如何获取“S”和“M”并将其保存到另一个字符串中?
由于您似乎仍然想知道评论中的人都是关于什么的,所以我会给您一个 100% 正确的解决方案,但仍然不能满足您的实际需求:
string s1 = "SpendSS-S";
string s2 = "Spendss-M";
string s1s = String.Join("", s1.Where(x => x == 'S' || x == 'M'));
string s2s = String.Join("", s2.Where(x => x == 'S' || x == 'M'));
你真正想要的(但没有告诉)是连字符后的字符串。
您可以通过Split
ing 您的字符串并从结果字符串数组中获取所需的部分来实现此目的:
string s3 = "SpendSS-XL";
// split by hyphen
string[] a3 = s3.Split(new char[] { '-' });
// take string from index 1
string s3s = a3[1];
或者干脆
string s3s = s3.Split(new char[] { '-' })[1];
有关详细信息和选项,请参阅String.Split。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句