如何过滤字符串 [英] How to filter strings

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

问题描述





在下面的代码中,我将所有第二行数据集写入excel文件。



Hi,

In the following code I am writing all the 2nd row of dataset into excel file.

int i = 1;
foreach (DataRow row in lstDS[0].Tables[0].Rows)
{
   xlWorkSheet1.Cells[i, 1] = row[2];
   i++;
}





但我需要以只有以GW开头的字符串写入excel表的方式过滤它们吗? ????





谢谢

John



But I need to filter them in a way that only strings starting with GW should be written into excel sheet????


Thanks
John

推荐答案

foreach (DataRow row in lstDS[0].Tables[0].Rows)
{
    string fieldFromDB=row[2].toString();
    if (fieldFromDB.StartsWith("GW"))
    {
        xlWorkSheet1.Cells[i, 1] = row[2];
        i++;
   }
}


好吧,我会从谷歌搜索开始:

Google搜索String.StartsWith [ ^ ]



这将告诉您如何在字符串上使用StartsWith命令。



然后,您可以将其与行[2]的内容一起使用,看看它们是否符合您的条件,然后将其添加到单元格中。然后循环。



希望有所帮助: - )
Well, I would start with this Google search:
Google Search for String.StartsWith[^]

This will tell you how to use the "StartsWith" command on a string.

You can then use this with the contents of row[2] and see if they meet your criteria and in turn add it to the cell. then loop.

Hope that helps :-)


你好,

你可以简单使用文本分割器方法;

Hi there,
You can simply use text spliter method;
string[] st = System.Text.RegularExpressions.Regex.Split(row, "GW");





祝你好运,

z3ngew



Good Luck,
z3ngew


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

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