如何获得字符数直到某个字符C#
我试图获得字符串的完整路径,如下所示:如何获得字符数直到某个字符C#,c#,count,substring,string-length,C#,Count,Substring,String Length,我试图获得字符串的完整路径,如下所示: ksh /u01/Utilities/SSL_Certificates/TestCert_20170724.sh 但我有一个问题,而且我 /u01/Utilities/SSL_Certificates/Tes 这是因为从ksh 如何获取从0到“/”的第一个索引的计数 我所拥有的是: string SSL_configuration_Path = ShellCommand.Substring(ShellCommand.IndexOf("/"), Shel
ksh /u01/Utilities/SSL_Certificates/TestCert_20170724.sh
但我有一个问题,而且我
/u01/Utilities/SSL_Certificates/Tes
这是因为从ksh
如何获取从0到“/”的第一个索引的计数
我所拥有的是:
string SSL_configuration_Path = ShellCommand.Substring(ShellCommand.IndexOf("/"), ShellCommand.LastIndexOf("/"));
第二个参数是多少个字符 不是哪个字符是最后一个
string SSL_configuration_Path = ShellCommand.Substring(
ShellCommand.IndexOf("/"),
ShellCommand.LastIndexOf("/") - ShellCommand.IndexOf("/"));
这并不是一个好的解决方案,但它应该解释您做错了什么以及为什么它不起作用。尝试使用专门为处理目录和文件名而设计的
Parh
类:
string ShellCommand = "ksh /u01/Utilities/SSL_Certificates/TestCert_20170724.sh";
string path = Path.GetDirectoryName(ShellCommand
.Substring(line.IndexOfAny(new char[] {
Path.AltDirectorySeparatorChar,
Path.DirectorySeparatorChar })));
Console.WriteLine(path);
结果:
\u01\Utilities\SSL_Certificates
请注意,您可以使用
/
或\
作为direcory分隔符,并获得标准化的最终结果(即使用Path.directoryseportorchar
separator)现在我得到的是/u01/Utilities/SSL\u Certificates/TestCer
第二个参数是长度?那你不应该用减法符号代替加号吗?交换条件?像这样:ShellCommand.LastIndexOf(“/”)-ShellCommand.IndexOf(“/”)
你是对的,我只是用减法,它就成功了!非常感谢。这就是为什么它通常有助于检查第一个。