控制台上的行分割数组?

发布于 2024-11-09 17:12:01 字数 451 浏览 3 评论 0原文

举个例子,我声明了一个字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧时光的容颜 2024-11-16 17:12:01

在这种特殊情况下,您可以使用 Version 类。

var master = "1.2.3.4";
var version = new Version(master);

var major = version.Major;
var minor = version.Minor;
var build = version.Build;
var revision = version.Revision;

In this particular case you can use Version class.

var master = "1.2.3.4";
var version = new Version(master);

var major = version.Major;
var minor = version.Minor;
var build = version.Build;
var revision = version.Revision;
那请放手 2024-11-16 17:12:01

假设您想用 '.' 分割字符串以获得主要、次要、构建等

string master = "1.2.3.4";
string [] arr = master.Split('.');

 string major = arr [0];         
string minor = arr [1];         
string project = arr [2];         
 string build = arr [3]; 

Assuming that you want to split the string by '.' to get major, minor, build etc

string master = "1.2.3.4";
string [] arr = master.Split('.');

 string major = arr [0];         
string minor = arr [1];         
string project = arr [2];         
 string build = arr [3]; 
颜漓半夏 2024-11-16 17:12:01

尝试一下

string master = "1.2.3.4";

string major = master.Split('.')[0];
string minor = master.Split('.')[1].;
string project = master.Split('.')[2];
string build = master.Split('.')[3];

string master = "1.2.3.4";
string [] arrayvar = master.Split('.');

 string major = arrayvar [0];         
string minor = arrayvar [1];         
string project = arrayvar [2];         
 string build = arrayvar [3]; 

Try Like

string master = "1.2.3.4";

string major = master.Split('.')[0];
string minor = master.Split('.')[1].;
string project = master.Split('.')[2];
string build = master.Split('.')[3];

or

string master = "1.2.3.4";
string [] arrayvar = master.Split('.');

 string major = arrayvar [0];         
string minor = arrayvar [1];         
string project = arrayvar [2];         
 string build = arrayvar [3]; 
本王不退位尔等都是臣 2024-11-16 17:12:01

我的拙见是,我不知道你的方法有什么问题。

我要指出的是,这是正确的做法。

在其他情况下,您需要使用正则表达式来识别某些文本的部分,因为您想要验证此类输入是否应采用某种特定格式等。这是使用正则表达式匹配组,但我相信你做得对,任何其他方法都将是一种矫枉过正。

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.

┊风居住的梦幻卍 2024-11-16 17:12:01

我认为对于这种情况,拆分是最好的。

但如果您也在探索其他选项,您可以使用正则表达式来拆分如下内容:

string[] lines = Regex.Split(master, "(\\.)");

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 :

string[] lines = Regex.Split(master, "(\\.)");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文