将逗号前的值存储在声明的变量C#中 [英] Getting values before comma to be stored in a declared variable C#

查看:96
本文介绍了将逗号前的值存储在声明的变量C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#代码从注册表中获取值(从注册表中读取的值包括逗号,例如12,32,156)。



我想将12存储在一个带整数X的变量中,第二个变量Y存储32个值,最后156存储在Z整数变量中。



到目前为止我做的是:





I am working on a C# code to get a value from the registry (the value that is read from the registry includes commas e.g. 12,32,156).

I would like to store 12 in a variable with integer X, and the second variable Y to store the 32 value, finally 156 to be stored inside the Z integer variable.

What I did so far is this:


string Get = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\KeyName\KeyName2\KeyName3", "ObjectBts", "").ToString();
MessageBox.Show(Get);
Get.ToCharArray();
int i = 0;

while (Get[i] != '\0')
{
    if (Get[i] == ',')
    {
        i++;
        X += Get[i];
//I would like to get the other value that is after the first comma to be stored in Y (which has to be 32

// Also I would like the third value to be stored in the Z integer (which has to be 156)
    }
    else
    {
        i++;
    }
}

推荐答案

使用拆分函数将这个值存储到数组中。

use the split the function this will stores the value into an array.
var data=Get.Split(',');
int X=Convert.ToInt32(data[0]);
int Y=Convert.ToInt32(data[1]);
int Z=Convert.ToInt32(data[2]);


var Getstring = str.Split(new [] { ',' }, 2)[0]



2确保是否有很多逗号


2 ensures if there are lots and lots of commas


这篇关于将逗号前的值存储在声明的变量C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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