字符串取值问题

fangfei306 2006-10-26 03:07:00
比如一个字符串“chinaren”,我要从右边开始取值得到"ren",
在VB或Asp中,
Right("chinaren",3) 即可得到ren
在C#中有没有什么方法,我找了半到也没找到相关的函数,只有跑到这里请求各位大虾的帮忙,万分感谢。
...全文
179 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangfei306 2006-10-26
  • 打赏
  • 举报
回复
补充一下。
我的字符串条件是字符串长度是不定的,会变化,可能三位,可20位。
private string GetContent(string str,string start,string last,int n)
{
以下两行是ASP里的写法。我需要转化成C#
getcontent=Right(str,Len(str)-Instr(LCase(str),LCase(start))-Len(start)+1) ;
getcontent=Left(GetContent,Instr(LCase(GetContent),LCase(last))-1);
}

上面这两行代码表示的意思是:str 是得到的一串很长很长的字符串,start是开始查找的关键字,last是结束查找的关键字,n不作考虑,我要得到的是start与last两个关键字之间的一段字符串值 。
francis67 2006-10-26
  • 打赏
  • 举报
回复
string str1 = "chinaren";

Response.Write(str1.Substring(str1.Length-3));
fangfei306 2006-10-26
  • 打赏
  • 举报
回复
:),各位老兄很及时,非常感谢,你们所说的我都知道,
其实我要的是这样的效果:
private string GetContent(string str,string start,string last,int n)
{
以下两行是ASP里的写法。我需要转化成C#
getcontent=Right(str,Len(str)-Instr(LCase(str),LCase(start))-Len(start)+1) ;
getcontent=Left(GetContent,Instr(LCase(GetContent),LCase(last))-1);
}
liujia_0421 2006-10-26
  • 打赏
  • 举报
回复
string s = "Chinaren";
string subs= "ren";
int index = s.LastIndexOf(subs);
string re = s.Substring(index, subs.Length);
citypantheryan 2006-10-26
  • 打赏
  • 举报
回复
string s = "Chinaren";
string subs= "ren";
int index = s.LastIndexOf(subs);
string re = s.Substring(index, subs.Length);
应该加上转换大小写的方法,比较保险
zhangzengping 2006-10-26
  • 打赏
  • 举报
回复
反正就是 Substring(); IastIndexOf(), LastIndexOf(),Remove()的配合!

随心所欲吧!
yeerh 2006-10-26
  • 打赏
  • 举报
回复
string s = "Chinaren";
string subs= "ren";
int index = s.LastIndexOf(subs);
string re = s.Substring(index, subs.Length);
yeerh 2006-10-26
  • 打赏
  • 举报
回复
string s = "Chinaren";
int index = s.LastIndexOf("ren");
string re = s.Substring(index, subs.Length);
anuo06 2006-10-26
  • 打赏
  • 举报
回复
String myString = "abc";
bool test1 = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
bool test2 = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧