字符串处理,很简单的问题。(菜鸟级提问)

liuxibei1987 2009-04-29 03:01:01
比如这样一个字符串 E:\aaa\bbb\ccc\ddd\e.txt
如何操作这个字符串得到e.txt这样一个字符处, 也就是说要得分最右边一个\后面的字符串
...全文
114 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tailor_ms 2009-04-29
  • 打赏
  • 举报
回复
办法比较笨,但能实现.

string strPath = objDialog.FileName;
strPath = MSTextFileProcess.ConverseString(strPath);

//Get the DBScript file's name
int iIndex = strPath.IndexOf("\\");
if (iIndex != -1)
{
strPath = strPath.Substring(0, iIndex);
this.txtFileName.Text = MSTextFileProcess.ConverseString(strPath);
}
else
this.txtFileName.Text = objDialog.FileName;
HDNGO 2009-04-29
  • 打赏
  • 举报
回复
或者~~

Match mt = Regex.Match(@"E:\aaa\bbb\ccc\ddd\e.txt", @"[^\\]+$");
Console.WriteLine(mt.Value);
liuxibei1987 2009-04-29
  • 打赏
  • 举报
回复
我自己也做出来了, 不过用split分割在取最后一个,好复杂
最佳答案还是用substring(lastindexof+1)
多谢各位
yinseguiji 2009-04-29
  • 打赏
  • 举报
回复
string x=@"E:\aaa\bbb\ccc\ddd\e.txt ";

string result=x.split('\\')[x.split('\\').length-1];
编程有钱人了 2009-04-29
  • 打赏
  • 举报
回复
string sss = @"E:\aaa\bbb\ccc\ddd\e.txt ";
string test = sss.Substring(sss.LastIndexOf("\\")+1);
LoveLife_Go 2009-04-29
  • 打赏
  • 举报
回复
string str="E:\aaa\bbb\ccc\ddd\e.txt";
int index = str.LastIndexOf("\\")+1;
str = str.Substring(index);
HDNGO 2009-04-29
  • 打赏
  • 举报
回复
Regex re = new Regex(@".*\\(.*)", RegexOptions.Singleline);
string str = @"E:\aaa\bbb\ccc\ddd\e.txt";
Match mt = re.Match(str);
if (mt.Success)
{
Console.WriteLine(mt.Groups[1].ToString());
}
MOmo400 2009-04-29
  • 打赏
  • 举报
回复
string path = @"E:\aaa\bbb\ccc\ddd\e.txt ";
FileInfo info = new FileInfo(path);

path = path.Remove(0, info.DirectoryName.Length + 1);
MessageBox.Show(path);
liuxibei1987 2009-04-29
  • 打赏
  • 举报
回复
3L的string[] str = a.split('\'), 这样写会报错的, 提示字符中出现换行符反正这个地方的\这个符号不能直接写进去啊
jingsong2008 2009-04-29
  • 打赏
  • 举报
回复
注意转义
jingsong2008 2009-04-29
  • 打赏
  • 举报
回复
string a = E:\aaa\bbb\ccc\ddd\e.txt ;
string[] str = a.split('\'),然后循环得出
zgke 2009-04-29
  • 打赏
  • 举报
回复
如果是文件方式的使用

FileInfo _FileInfo = new FileInfo(@"E:\aaa\bbb\ccc\ddd\e.txt");

_FileInfo.Name; //e.txt

_FileInfo.Directory.FullName; //E:\aaa\bbb\ccc\ddd\
liuxibei1987 2009-04-29
  • 打赏
  • 举报
回复
who can help me?
ztwz 2009-04-29
  • 打赏
  • 举报
回复
获取最后一个\位置+1就ok了。自己查msdn

111,126

社区成员

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

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

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