如下面的类型的字符串,我想得到结果类型的字符串怎么做;我想取从右边起的第一个-前的字符串??
  原字符串:asdf152-s-01 结果:asdf152-s
  原字符串:asdf152-01 结果:asdf152

解决方案 »

  1.   

                string a = "asdf152-s-01";
                int index = a.LastIndexOf('-');
                if (index > -1)
                {
                    string suba = a.Substring(0, index);
                }
      

  2.   

    使用Regex,如下:public class SubStringRegex
        {
            string pattern = @"-\w*$";
            string[] testSource = new string[] { "asdf152-s-01", "asdf152-01","aksd"};        public void TestMatch()
            {
                System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(pattern);
                foreach (string s in testSource)
                {
                    Console.WriteLine("Source:{0}\tMatched:{1}", s, r.Replace(s, ""));
                }
            }
        }
      

  3.   

    string a="asdf152-s-01";
    string[] aa=a.spit('-');
    string youneed=aa[0];//这是你要的
      

  4.   

    string a="asdf152-s-01";
    string[] aa=a.spit('-');
    string youneed=aa[0];//这是你要的
      

  5.   

    string a="asdf152-s-01"; 
    string[] aa=a.split('-'); //这个写错了下
    string youneed=aa[0];//这是你要的
      

  6.   

    string originalString;
    originalString = "asdf150-s-01";int lastItemIndex;
    lastItemIndex = originalString.LastIndexOf('-');string resultString;
    resultString = originalString.Split(0, lastItemIndex);
      

  7.   

                string a = "asdf152-s-01";
                int index = a.LastIndexOf('-');
                int index1 = a.IndexOf('-');
                if (index > -1)
                {
                    string str = a.Substring(0, index);
                    string str1 = a.Substring(0,index1);
                    Console.Write(str);
                    Console.Write(str1);
                }
      

  8.   

    string a="asdf152-s-01"; 
    string[] aa=a.spit('-'); 
    string youneed=aa[0];//这是你要的
    //晕,只是把'-'去掉呀
    int res=0//用于记录'-'的次数
    string Result;
    for(int i=0;i<aa.lenght;i++)
    {
    if(aa[i].lenght==1&&res<1)
    {
     if((char)aa[i].Equal('-"))
     {
       res+=1;
     }
    }
    Result+=aa[i];//这个可以了}
      

  9.   

    string a="asdf152-s-01"; 
    string[] aa=a.spit('-'); 
    string youneed=aa[0];//这是你要的 
    //晕,只是把'-'去掉呀 
    int res=0//用于记录'-'的次数 
    string Result; 
    for(int i=0;i <aa.lenght;i++) 

    if(aa[i].lenght==1&&res <1) 

    Result+=aa[i];//这个可以了,应该放这里
    if((char)aa[i].Equal('-")) 

      res+=1; 
      


    }
      

  10.   

        string strTemp = "asdf152-s-01"; 
        int intPos = 0; 
        intPos = strTemp.LastIndexOf("-"); 
        if (intPos > -1) { 
            strTemp = strTemp.Substring(0, strTemp.LastIndexOf("-")); 
        } 
      

  11.   

    不好意思,看错了,上面是错的,正确的如下:
    string str = "asdf150-s-01";
    int index = str.LastIndexOf("-");
    int length=str.Length-str.Substring(index).Length;
    string myStr = str.Substring(0, length);
    Response.Write(myStr);
      

  12.   

    LastIndexOf是最后一个匹配项的索引位置,因为从0开始,位置直接做长度就可以了(因为索引值是从0开始的,索引位置加1就是,匹配项的位置,再减去1就是前面字符的长度)
      

  13.   

    LastIndexOf是最后一个匹配项的索引位置,因为从0开始,位置直接做长度就可以了(因为索引值是从0开始的,索引位置加1就是截取到含匹配项的字符串的长度,再减去1就是前面字符的长度)
      

  14.   


                string str = "asdf152-s-01";
                string result = str.Substring(0, str.LastIndexOf("-"));//result 就是所求
      

  15.   

    18楼的不太严谨,修改一下:            string str = "asdf152-asdfljsa-werew-47sdlf";
                string result = str.Substring(0, str.LastIndexOf("-") > -1 ? str.LastIndexOf("-") : str.Length);//result 就是所求