Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 断开C上最后一个点上的字符串的最佳方法#_C#_String - Fatal编程技术网

C# 断开C上最后一个点上的字符串的最佳方法#

C# 断开C上最后一个点上的字符串的最佳方法#,c#,string,C#,String,在最后一个点的位置将字符串一分为二的最佳方式和更惯用的方式是什么?基本上将扩展名与文件路径或URL中的其他路径分离。到目前为止,我正在做的是拆分(“.”),然后是字符串。除了最后一部分以外的所有内容的Join(“.”)。听起来像是用火箭筒杀死苍蝇。您可以使用Path.GetFilenameWithoutExtension() 或者如果这对你不起作用: int idx = filename.LastIndexOf('.'); if (idx >= 0) filename = file

在最后一个点的位置将字符串一分为二的最佳方式和更惯用的方式是什么?基本上将扩展名与文件路径或URL中的其他路径分离。到目前为止,我正在做的是拆分(“.”),然后是字符串。除了最后一部分以外的所有内容的Join(“.”)。听起来像是用火箭筒杀死苍蝇。

您可以使用Path.GetFilenameWithoutExtension()

或者如果这对你不起作用:

int idx = filename.LastIndexOf('.');

if (idx >= 0)
   filename = filename.Substring(0,idx);
这里的string方法可能对您有所帮助


但是Path或FileInfo操作符更适合基于文件名的操作。

我认为您真正想要的只是:

string input = "foo.bar.foobar";
int lastDotPosition = input.LastIndexOf('.');

if (lastDotPosition == -1)
{
    Console.WriteLine("No dot found");
}
else if (lastDotPosition == input.Length - 1)
{
    Console.WriteLine("Last dot found at the very end");

}
else
{
    string firstPart = input.Substring(0, lastDotPosition);
    string lastPart = input.Substring(lastDotPosition + 1);

    Console.WriteLine(firstPart);
    Console.WriteLine(lastPart);
}

要获取不带扩展名的路径,请使用

System.IO.Path.GetFileNameWithoutExtension(fileName)
要获得延伸(包括圆点),请使用

编辑:

不幸的是,GetFileName WithoutExtension会从前导路径中剥离,因此您可以使用:

if (path == null)
{
    return null;
}
int length = path.LastIndexOf('.');
if (length == -1)
{
    return path;
}
return path.Substring(0, length);

应该会对您有所帮助。

如果您需要性能,请选择以下选项:

    string s = "a.b.c.d";
    int i = s.LastIndexOf('.');
    string lhs = i < 0 ? s : s.Substring(0,i),
        rhs = i < 0 ? "" : s.Substring(i+1);
string s=“a.b.c.d”;
int i=s.LastIndexOf('.');
字符串lhs=i<0?s:s.子串(0,i),
rhs=i<0?“”s.子串(i+1);

使用LastIndexOf方法返回字符最后找到的位置怎么样。然后可以使用子字符串提取所需内容。

String.LastIndexOf将返回点的位置(如果点在字符串中存在)。然后,您可以使用String.Substring方法拆分字符串。

您可以使用String的方法


LastIndexOf和substring来完成任务。

这对URL片段有效吗(使用/代替\)?它还会返回“文件夹”吗?Path.GetFilename()只返回文件名,不返回目录AFAIK。如果OP只询问文件路径,则返回+1。但是System.IO.Path文档没有提到对URL的支持。正确,但如果文件名不包含路径,它可能对您有效。它可能有效,但不幸的是,这意味着依赖于未记录的功能。如果这对您来说没问题,System.IO.Path就是问题的解决方案。如果人们想知道Path.GetExtension等为什么不起作用:各种路径函数假定您使用的是Windows路径。。。因此,如果字符串包含任何无效字符,它们就会抛出,如'
    string s = "a.b.c.d";
    int i = s.LastIndexOf('.');
    string lhs = i < 0 ? s : s.Substring(0,i),
        rhs = i < 0 ? "" : s.Substring(i+1);