Csharp/C#教程:C#将字符串拆分为单独的变量分享


C#将字符串拆分为单独的变量

我发现在找到逗号时,我试图将字符串拆分为单独的字符串变量。

string[] dates = line.Split(','); foreach (string comma in dates) { string x = // String on the left of the comma string y = // String on the right of the comma } 

我需要能够在逗号的每一侧为字符串创建一个字符串变量。 谢谢。

在这种情况下摆脱ForEach。

只是:

 string x = dates[0]; string y = dates[1]; 

只需从数组中获取字符串:

 string[] dates = line.Split(','); string x = dates[0]; string y = dates[1]; 

如果可能有多个逗号,则应指定您只需要两个字符串:

 string[] dates = line.Split(new char[]{','}, 2); 

另一种方法是使用字符串操作:

 int index = lines.IndexOf(','); string x = lines.Substring(0, index); string y = lines.Substring(index + 1); 

你的意思是这样的吗?

上述就是C#学习教程:C#将字符串拆分为单独的变量分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  string x = dates[0]; string y = dates[1]; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1019219.html

(0)
上一篇 2022年1月4日
下一篇 2022年1月4日

精彩推荐