取字符串的问题 如下面的类型的字符串,我想得到结果类型的字符串怎么做;我想取从右边起的第一个-前的字符串?? 原字符串:asdf152-s-01 结果:asdf152-s 原字符串:asdf152-01 结果:asdf152 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string a = "asdf152-s-01"; int index = a.LastIndexOf('-'); if (index > -1) { string suba = a.Substring(0, index); } 使用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, "")); } } } string a="asdf152-s-01";string[] aa=a.spit('-');string youneed=aa[0];//这是你要的 string a="asdf152-s-01";string[] aa=a.spit('-');string youneed=aa[0];//这是你要的 string a="asdf152-s-01"; string[] aa=a.split('-'); //这个写错了下string youneed=aa[0];//这是你要的 string originalString;originalString = "asdf150-s-01";int lastItemIndex;lastItemIndex = originalString.LastIndexOf('-');string resultString;resultString = originalString.Split(0, lastItemIndex); 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); } 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];//这个可以了} 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; } } } string strTemp = "asdf152-s-01"; int intPos = 0; intPos = strTemp.LastIndexOf("-"); if (intPos > -1) { strTemp = strTemp.Substring(0, strTemp.LastIndexOf("-")); } 不好意思,看错了,上面是错的,正确的如下: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); LastIndexOf是最后一个匹配项的索引位置,因为从0开始,位置直接做长度就可以了(因为索引值是从0开始的,索引位置加1就是,匹配项的位置,再减去1就是前面字符的长度) LastIndexOf是最后一个匹配项的索引位置,因为从0开始,位置直接做长度就可以了(因为索引值是从0开始的,索引位置加1就是截取到含匹配项的字符串的长度,再减去1就是前面字符的长度) string str = "asdf152-s-01"; string result = str.Substring(0, str.LastIndexOf("-"));//result 就是所求 18楼的不太严谨,修改一下: string str = "asdf152-asdfljsa-werew-47sdlf"; string result = str.Substring(0, str.LastIndexOf("-") > -1 ? str.LastIndexOf("-") : str.Length);//result 就是所求 c#多维数组问题 刚在书上看到这么个选择题!.NET框架结构的核心组件是( ). C# 字符串强制转换为整型 lock原理 C#中查询指定年份记录的SQL语句 c/s邮件客户端问题 一个查询语句牵扯多个表的问题 这个存储过程怎么不行呀? 小弟有问题 请教:ado.net GetRows(20, adBookmarkCurrent) C# winform 取消按钮 如何取消textbox里修改内容 窗体传直问题
int index = a.LastIndexOf('-');
if (index > -1)
{
string suba = a.Substring(0, index);
}
{
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, ""));
}
}
}
string[] aa=a.spit('-');
string youneed=aa[0];//这是你要的
string[] aa=a.spit('-');
string youneed=aa[0];//这是你要的
string[] aa=a.split('-'); //这个写错了下
string youneed=aa[0];//这是你要的
originalString = "asdf150-s-01";int lastItemIndex;
lastItemIndex = originalString.LastIndexOf('-');string resultString;
resultString = originalString.Split(0, lastItemIndex);
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);
}
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];//这个可以了}
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;
}
}
}
int intPos = 0;
intPos = strTemp.LastIndexOf("-");
if (intPos > -1) {
strTemp = strTemp.Substring(0, strTemp.LastIndexOf("-"));
}
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);
string str = "asdf152-s-01";
string result = str.Substring(0, str.LastIndexOf("-"));//result 就是所求
string result = str.Substring(0, str.LastIndexOf("-") > -1 ? str.LastIndexOf("-") : str.Length);//result 就是所求