[英]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
類:
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.DirectorySeparatorChar
分隔符)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.