如何在C#中拆分字符串 [英] how to split string in C#

查看:75
本文介绍了如何在C#中拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下拉列表中显示所有项目名称如下



string sourceDirectory = Server.MapPath(〜/);

DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectory);

var aspxFiles = new List< string>(Directory.GetFiles(sourceDirectory,* .aspx,SearchOption.AllDirectories));



foreach(字符串currentFile in aspxFiles)

{

this.ddllink.Items.Add(currentFile);

}



当我在下拉列表输出中运行如下



E:Projects \接待'\\Default.aspx





但我想在下拉列表中显示如下Default.aspx



i希望在\之后拆分。



我怎样才能在asp.net中使用c#。



在下拉列表中我想要输出如下

Default.aspx

I am displaying all the project name in dropdownlist as follows

string sourceDirectory = Server.MapPath("~/");
DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectory);
var aspxFiles = new List<string>(Directory.GetFiles(sourceDirectory, "*.aspx", SearchOption.AllDirectories));

foreach (string currentFile in aspxFiles)
{
this.ddllink.Items.Add(currentFile);
}

When i run in dropdownlist output as follows

E:Projects\Reception\Default.aspx


But i want in dropdownlist as follows Default.aspx

i want to split after \.

for that how can i do in asp.net using c#.

In dropdownlist i want output as follows
Default.aspx

推荐答案

您好,

System.IO.Path.GetFileName(字符串filenameincludingfilepath)将最终给你文件名

Hello ,
System.IO.Path.GetFileName(string filenameincludingfilepath) will give you the filename ultimately
var name=@"E:Projects\Reception\Default.aspx\default1.aspx";
Console.WriteLine(System.IO.Path.GetFileName(name));		



和OP将是


and OP will be

default1.aspx





谢谢



Thanks


使用

Use
string sourceDirectory = Server.MapPath("~/");
DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectory);
var aspxFiles = new List(Directory.GetFiles(sourceDirectory, "*.aspx",SearchOption.AllDirectories));
foreach (string currentFile in aspxFiles)
{
this.ddllink.Items.Add(Path.GetFileName(currentFile));
}


这篇关于如何在C#中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆