如何在变量C#中分隔double和string [英] How to separate double and string in a variable C#

查看:100
本文介绍了如何在变量C#中分隔double和string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我在搜索互联网时还没有看到过这种问题。



有没有办法在存储时分离double和string在单个字符串变量??



例如我有



I think I haven't seen this kind of problems yet when i searched the internet.

is there a way to seperate double and string when they are stored in a single string variable??

for example i have

string blabla = "4.2 Minutes";





我想知道的是例如。 4.2将存储在变量数字中,分钟将存储在变量字母中。

可能吗?



我尝试过的事情:



我尝试过使用Regex。





what I want to know is that for example. 4.2 will be stored in variable "numbers" and Minutes will be stored in variable "Letters".
is that possible?

What I have tried:

I have tried using Regex.

string cutie = "4.5 Minutes";
Regex re = new Regex(@"([a-zA-Z]+)(\d+)");
Match result = re.Match(cutie);
string letters = result.Value;
string numbers = result.Value;





well something像这样,它不起作用。



well something like this, tho it doesn't work.

推荐答案

如果想要仍然想要使用正则表达式,请尝试以下方法。

If want still want to use a regex try the following.
String blabla = "4.2 Minutes";

Regex regex = new Regex(@"^(?<NUMVALUE>\d*.\d*)\s*(?<STRVALUE>[A-Z][a-z]*)


,RegexOptions.Singleline);

匹配匹配= regex.Match(blabla);

if(match.Success)
{
String numbers = match.Groups [NUMVALUE]。Value;
String letters = match.Groups [STRVALUE]。Value;

Console.WriteLine(数字);
Console.WriteLine(字母);
}
其他
{
Console.WriteLine(不匹配);
}
", RegexOptions.Singleline); Match match = regex.Match(blabla); if(match.Success) { String numbers = match.Groups["NUMVALUE"].Value; String letters = match.Groups["STRVALUE"].Value; Console.WriteLine(numbers); Console.WriteLine(letters); } else { Console.WriteLine("Not matched"); }






举个例子,上面,执行此操作:



Hi,

To your example, above, do this:

private bool is_double(string val)
{
    try
    {
        Convert.ToDouble(val);
        return true;
    }
    catch
    {
        return false;
    }
}

private void separate_word()
{
   string text = "4.2 Minutes";
   string[] arr = text.Split(' ');

   string letter, number;

    foreach(string item in arr)
    {
        if(this.is_double(item))
        {
            number = item;
        }
        else
        {
            letter = item;
        }
    }
}


这篇关于如何在变量C#中分隔double和string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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