如何获得特定字符 C# 之前的字符数
How to get a count of characters until certain character C#
我正在尝试获取字符串的完整路径,如下所示:
ksh /u01/Utilities/SSL_Certificates/TestCert_20170724.sh
但我遇到了问题,我得到了
/u01/Utilities/SSL_Certificates/Tes
那是因为要从 ksh
中获取 4 个字符
如何获取从 0 到“/”的第一个索引的计数
我有的是这个:
string SSL_configuration_Path = ShellCommand.Substring(ShellCommand.IndexOf("/"), ShellCommand.LastIndexOf("/"));
第二个参数是多少个字符。
不是最后一个字符。
string SSL_configuration_Path = ShellCommand.Substring(
ShellCommand.IndexOf("/"),
ShellCommand.LastIndexOf("/") - ShellCommand.IndexOf("/"));
并不是说这是一个好的解决方案,但它应该解释你做错了什么以及为什么它不起作用。
尝试使用专为处理目录和文件名称而设计的 Parh
class:
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
请注意,您可以使用 /
或 \
作为目录分隔符并使最终结果标准化(即使用 Path.DirectorySeparatorChar
分隔符)
我正在尝试获取字符串的完整路径,如下所示:
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
class:
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
请注意,您可以使用 /
或 \
作为目录分隔符并使最终结果标准化(即使用 Path.DirectorySeparatorChar
分隔符)