C#分隔字符串和整数 [英] c# separate string and int

查看:79
本文介绍了C#分隔字符串和整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一家药店的项目.

我需要提供包装价值的地方10mg,2 mg,10S,其中存在int以及字符串值.

是否可以将这两个值分开并将它们放在不同的变量中.
我不能使用Substring(),因为int和string的长度不相同.

请帮助我.

I am developing a project of a medicine shop.

where I need to provide pack value e.g. 10mg, 2 mg , 10S where int as well as string value is there.

Is it possible to separate these two values and place these in different variable.
I can''t use Substring() because the length of int and string are not same.

Please help me.

推荐答案

我会使用正则表达式:
I''d use a Regex:
Regex regex = new Regex("(?<Number>\\d+)(?<Unit>.*)");
Match m = regex.Match("10mg");
if (m.Success)
    {
    int number = int.Parse(m.Groups["Number"].Value);
    string unit = m.Groups["Unit"].Value;
    ...
    }


使用Regex:

这篇关于C#分隔字符串和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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