c#字符串如何截取 从指定字符串开始 指定字符串结束?

zkeenly 2015-01-01 08:53:26
如题中所描述。有什么现成的方法吗?或者应该怎么做?
...全文
71434 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
john_QQ:2335298917 2015-11-24
  • 打赏
  • 举报
回复
引用 1 楼 wind_cloud2011 的回复:
你读取指定字符在字符串的位置起始位置及结束位置,再用substring, 如: string s = "1234abc123444555efcdeeee"; int i = s.IndexOf("abc")+1; int j = s.IndexOf("ef"); string str = s.Substring(i, j - i+2);
顶一下
江南小鱼 2015-11-24
  • 打赏
  • 举报
回复
SubString不就是干这活的方法么
gtclulong 2015-11-24
  • 打赏
  • 举报
回复
更新一下
private string GetStr(string TxtStr, string FirstStr, string SecondStr)
        {
            if (FirstStr.IndexOf(SecondStr, 0) != -1)
                return "";
            int FirstSite = TxtStr.IndexOf(FirstStr, 0);
            int SecondSite = TxtStr.IndexOf(SecondStr, FirstSite + 1);
            if (FirstSite == -1 || SecondSite == -1)
                return "";
            return TxtStr.Substring(FirstSite + FirstStr.Length, SecondSite - FirstSite - FirstStr.Length);
        }
gtclulong 2015-11-24
  • 打赏
  • 举报
回复
private string GetStr(string TxtStr, string FirstStr, string SecondStr)
        {
            if (FirstStr.IndexOf(SecondStr, 0) != -1)
                return "";
            int FirstSite = TxtStr.IndexOf(FirstStr, 0);
            int SecondSite = TxtStr.IndexOf(SecondStr, FirstSite + 1);
            return TxtStr.Substring(FirstSite + FirstStr.Length, SecondSite - FirstSite - FirstStr.Length);
        }
第一个字串不能包含第二个字串
Cbh5425 2015-11-24
  • 打赏
  • 举报
回复
引用 3 楼 zhangkaixu321 的回复:
[quote=引用 1 楼 wind_cloud2011 的回复:] 你读取指定字符在字符串的位置起始位置及结束位置,再用substring, 如: string s = "1234abc123444555efcdeeee"; int i = s.IndexOf("abc")+1; int j = s.IndexOf("ef"); string str = s.Substring(i, j - i+2);
这个方法挺好的,感谢
eew_1679694743 2015-01-02
  • 打赏
  • 举报
回复
C#如何截取字符串问题 static void Main(string[] args) { //定义一个字符串,用来存储文件全路径 string strAllPath = "D:\\科普书籍大全(普及版)\\百科全书(中文版).exe"; string strPath = strAllPath.Substring(0, strAllPath.LastIndexOf("\\") + 1);//获取文件路径 string strName = strAllPath.Substring(strAllPath.LastIndexOf("\\") + 1);//获取文件名 Console.WriteLine("文件路径:" + strPath);//显示文件路径 Console.WriteLine("文件名:" + strName);//显示文件名 Console.ReadLine(); }
SmileSkyNet 2015-01-02
  • 打赏
  • 举报
回复
其实还有一个方法也是常用的LastIndexof() 这个方法就是获取最后一个指定的字符位置,和上面的一起用效果比较好
allen0118 2015-01-02
  • 打赏
  • 举报
回复
楼上的说的很明白了,直接indexof获取到开始和结束两个字符串的位置,然后substring完事。
zkeenly 2015-01-01
  • 打赏
  • 举报
回复
引用 1 楼 wind_cloud2011 的回复:
你读取指定字符在字符串的位置起始位置及结束位置,再用substring, 如: string s = "1234abc123444555efcdeeee"; int i = s.IndexOf("abc")+1; int j = s.IndexOf("ef"); string str = s.Substring(i, j - i+2);
这个方法确实不错。多谢。
wind_cloud2011 2015-01-01
  • 打赏
  • 举报
回复
int i = s.IndexOf("abc"); str结果为:abc123444555ef,
wind_cloud2011 2015-01-01
  • 打赏
  • 举报
回复
你读取指定字符在字符串的位置起始位置及结束位置,再用substring, 如: string s = "1234abc123444555efcdeeee"; int i = s.IndexOf("abc")+1; int j = s.IndexOf("ef"); string str = s.Substring(i, j - i+2);

110,539

社区成员

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

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

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