控制台上的行分割数组?
举个例子,我声明了一个字符串:
string master = "1.2.3.4";
其形式为:
major.minor.project.build
我如何获取字符串“major”、“minor”、“project”和“ build”与字符串“master”分开分配?
我知道我们必须进行 line.split 这就是我尝试过的:
string[] master = line.Split('.');
string major = master[0];
string minor = master[1];
string project = master[2];
string build = master[3];
lets take for example i have a string declared:
string master = "1.2.3.4";
which is in the form of:
major.minor.project.build
how do i get a string "major","minor","project" and "build" assigned separately from the string "master"?
I know we have to do line.split and this is what i have tried:
string[] master = line.Split('.');
string major = master[0];
string minor = master[1];
string project = master[2];
string build = master[3];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这种特殊情况下,您可以使用
Version
类。In this particular case you can use
Version
class.假设您想用
'.'
分割字符串以获得主要、次要、构建等Assuming that you want to split the string by
'.'
to get major, minor, build etc尝试一下
或
Try Like
or
我的拙见是,我不知道你的方法有什么问题。
我要指出的是,这是正确的做法。
在其他情况下,您需要使用正则表达式来识别某些文本的部分,因为您想要验证此类输入是否应采用某种特定格式等。这是使用正则表达式匹配组,但我相信你做得对,任何其他方法都将是一种矫枉过正。
My humild opinion is I don't know what you find wrong in your approach.
I'm going to point you that this is the right way of doing this.
There're other situations where you'll need to identify parts of some text by using regular expressions because you want to validate that such input should be in some specific format or so. That's using regular expression match groups, but I believe you're doing right and any other approach would be an overkill.
我认为对于这种情况,拆分是最好的。
但如果您也在探索其他选项,您可以使用正则表达式来拆分如下内容:
I think for this case, split is the best.
But in case you are exploring other options also ,You can use regex to split something like this :