.net c# split 用子字符串分割字符串

.net c# split 用子字符串分割字符串

string str="select * from tree where treetop=1";

目的:要将str用"where"分割

错误做法:

     string [] s = str.split( new   char [ 5 ] {'w','h','e','r','e'} ); 

    
string  tmp = "" ;

    
foreach ( string  ss  in  s)

        tmp
+= " * " + ss.ToString() + " # " ;

    Label1.Text
= tmp; 

 结果:

*s#*l#*ct * f#*om t#*#*#* #*#*#*#*#* t#*#*#*top=1#

正确做法:结果:

     string [] s = str.Split( new   string [ 1 "where" } , System.StringSplitOptions.None);

    
string  tmp = "" ;

    
foreach ( string  ss  in  s)

        tmp
+= " * " + ss.ToString() + " # " ;

    Label1.Text
= tmp; 

 

*select * from tree #* treetop=1#

还有一种方法是可行的,就是正则表达式。

using  System.Text.RegularExpressions;
    
protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
string content="agcsmallmacsmallgggsmallytx";
        
string[] resultString = Regex.Split(content, "small", RegexOptions.IgnoreCase);
        content
="";
        
foreach (string i in resultString)
            content 
+= i+" ";
        Label1.Text 
=content;
    }


输出"agc mac ggg ytx"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值