如何从字符串中拆分int和其他字符 [英] How to split int and other characters from a string

查看:63
本文介绍了如何从字符串中拆分int和其他字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个列的表(tbl_product_price)(product_id,product_price,quantity)



表值将是这样的,

I have table(tbl_product_price) with three columns (product_id,product_price,quantity)

The table value will be like this,

product_id     product_price     quantity
   1             500              1Kg
   2             250              500gm
   3             100              12pcs
   4             150              12nos



我的sql查询是:


My sql query is:

select quantity from tbl_product_price where product_id=2



i希望输出数量为整数,我用C#中的数据集把它拿出来,

现在我想将数量列拆分成整数和字符。



例如,如果我选择product_id = 2,则500gm将分成,


i want output quantity as integer,i took it up with a dataset in C#,
now i want to split quantity column into integer and character.

For ex, if i select product_id=2, 500gm will split into,

int quantity1=500
string str1=gm



如果我选择product_id = 3,12pcs将分成,


if i select product_id=3, 12pcs will split into,

int quantity1=12
string str1=pcs



如何在C#中这样做?

我试过谷歌,但尚未找到完美的解决方案。



提前致谢,

Rajeshwaran T


How to do like this in C#?
I tried google, but not yet got perfect solution.

Thanks in advance,
Rajeshwaran T

推荐答案

正则表达式似乎是你需要的:

A regular expression seems to be what you need here:
using System.Text.RegularExpressions;

Regex regex = new Regex(@"^(?<Quantity>[0-9]+)(?<Unit>[^0-9]+)


,RegexOptions .Compiled | RegexOptions.CultureInvariant);

string input = 12颗;
匹配m = regex.Match(输入);
int quantity = int .Parse(m.Groups [ Quantity]。Value);
string unit = m.Groups [ 单元]值。

// quantity = 12
// unit =pcs
", RegexOptions.Compiled | RegexOptions.CultureInvariant); string input = "12pcs"; Match m = regex.Match(input); int quantity = int.Parse(m.Groups["Quantity"].Value); string unit = m.Groups["Unit"].Value; // quantity = 12 // unit = "pcs"



希望这有帮助。


Hope this helps.


另一种方法是使用 Linq [ ^ ] + Regex.Replace方法 [ ^ ]。



想象一下,您确实将数据加载到DataTable对象中,因此您可以将数量数据拆分为数量 (字符串)和单位(int):

Another way is to use Linq[^] + Regex.Replace method[^].

Imagine, you did load the data into DataTable object, so you're able to split quantity data into quantity(string) and unit(int):
DataTable dt  = new DataTable();
dt.Columns.Add(new DataColumn("id", typeof(int)));
dt.Columns.Add(new DataColumn("product_price", typeof(int)));
dt.Columns.Add(new DataColumn("quantity", typeof(string)));
dt.Rows.Add(new Object[]{1, 500, "1Kg"});
dt.Rows.Add(new Object[]{2, 250, "500gm"});
dt.Rows.Add(new Object[]{3, 100, "12pcs"});
dt.Rows.Add(new Object[]{4, 150, "12nos"});

string patternq = @"\d+";
string patternu = @"\B[A-Z]+";

var result = dt.AsEnumerable()
    .Select(x=>new
    {
        id = x.Field<int>("id"),
        price = x.Field<int>("product_price"),
        quantity = Convert.ToInt32(Regex.Replace(x.Field<string>("quantity"), patternu, string.Empty ,System.Text.RegularExpressions.RegexOptions.IgnoreCase)),
        unit = Regex.Replace(x.Field<string>("quantity"), patternq, string.Empty, System.Text.RegularExpressions.RegexOptions.None)
    }).ToList();

foreach(var obj in result)
{
    Console.WriteLine("{0}\t{1}\t{2}\t{3}", obj.id, obj.price, obj.quantity, obj.unit);
}


这篇关于如何从字符串中拆分int和其他字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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