Search code examples
c#countsubstringstring-length

How to get a count of characters until certain character C#


I'm trying to get the full path of a string as follow:

ksh /u01/Utilities/SSL_Certificates/TestCert_20170724.sh

but I'm having an issue and I'm getting

/u01/Utilities/SSL_Certificates/Tes

that's because is getting the 4 characters from ksh

how can I get the count starting from 0 to the first index of "/"

What I have is this:

string SSL_configuration_Path = ShellCommand.Substring(ShellCommand.IndexOf("/"), ShellCommand.LastIndexOf("/"));

Solution

  • Second parameter is how many characters.

    Not which character is the last.

    string SSL_configuration_Path = ShellCommand.Substring(
      ShellCommand.IndexOf("/"),
      ShellCommand.LastIndexOf("/") - ShellCommand.IndexOf("/"));
    

    Not that this is a good solution, but it should explain what you're doing wrong and why it didn't work.