关于字符串截取的问题

isme2013 2010-09-26 10:12:49
例如有这样一个字符串“PicLib/12.jpg”,我怎么才能截取到“PicLib”、“12”和“jpg”,这三部分我都要,就是把他们分开,如何截取? 谢谢!
...全文
117 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xu_diligent 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nocturnellj 的回复:]
string str = "PicLib/12.jpg";
int charIndex1 = str.IndexOf('/');
int charIndex2 = str.LastIndexOf('.');
string strPicLib = str.Substring(0, charIndex1);
string strName = str.Substring(charIndex1, ……
[/Quote]
这种是可以的,简单有效,你试了没?
viena 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qixing_gan 的回复:]
引用 8 楼 viena 的回复:
C# code
string str = "PicLib/12.jpg";
string[] ary = str.Split(new char[] {'/', '.'});

不行这样呀,因为完整的是一个路径,而"PicLib/12.jpg"只是一部分呀
[/Quote]
楼主该吃药了
dalmeeme 2010-09-26
  • 打赏
  • 举报
回复
		string s = "PicLib/12.jpg";
string s1 = s.Substring(0, s.LastIndexOf('/'));
string s2 = s.Substring(s.LastIndexOf('/')+1,s.LastIndexOf('.')-s.LastIndexOf('/')-1);
string s3 = s.Substring(s.LastIndexOf('.')+1);
Response.Write(s1);
Response.Write("<br/>");
Response.Write(s2);
Response.Write("<br/>");
Response.Write(s3);
娃ha哈 2010-09-26
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
string str = "PicLib/12.jpg";
int i = str.IndexOf("/",0);
int j = str.IndexOf(".", i);
string a = str.Substring(0, i);
string b = str.Substring(i+1, j - i-1);
string c = str.Substring(j+1, str.Length - j-1);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.ReadKey();
}

Canny 2010-09-26
  • 打赏
  • 举报
回复
够多了 我就不凑热闹了 接分就可以了!嘿嘿!
isme2013 2010-09-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 viena 的回复:]
C# code
string str = "PicLib/12.jpg";
string[] ary = str.Split(new char[] {'/', '.'});
[/Quote]
不行这样呀,因为完整的是一个路径,而"PicLib/12.jpg"只是一部分呀
viena 2010-09-26
  • 打赏
  • 举报
回复
string str = "PicLib/12.jpg";
string[] ary = str.Split(new char[] {'/', '.'});
NocturneLLJ 2010-09-26
  • 打赏
  • 举报
回复
string str = "PicLib/12.jpg";
int charIndex1 = str.IndexOf('/');
int charIndex2 = str.LastIndexOf('.');
string strPicLib = str.Substring(0, charIndex1);
string strName = str.Substring(charIndex1, charIndex2 - charIndex1);
string strPostfix = str.Substring(charIndex2 + 1);
wuyi8808 2010-09-26
  • 打赏
  • 举报
回复
    string str = "PicLib/12.jpg";
string[] ary = System.Text.RegularExpressions.Regex.Split(str, "[/.]");
wuyi8808 2010-09-26
  • 打赏
  • 举报
回复
    string str = "PicLib/12.jpg";
string[] ary = str.Split('/', '.');
兔子-顾问 2010-09-26
  • 打赏
  • 举报
回复
string s = "PicLib/12.jpg";
Console.WriteLine(Path.GetDirectoryName(s));
Console.WriteLine(Path.GetFileNameWithoutExtension(s));
Console.WriteLine(Path.GetExtension(s));
skyqingtian 2010-09-26
  • 打赏
  • 举报
回复
一个还不够吗?
isme2013 2010-09-26
  • 打赏
  • 举报
回复
谢谢,方法越多越好,呵呵呵
cpp2017 2010-09-26
  • 打赏
  • 举报
回复
  string str = "PicLib/12.jpg";
str = str.Replace("/", ".");
string[] ary = str.Split('.');

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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