A more efficient way of extracting a name from a string? Here is some code that reads Active Directory to get a person's manager and parses the result for the managers name. It omits the remaining information. I was just wondering if there might be some more efficient way of doing this instead of the code I am currently using.

var Manager = (string)UserDirectoryData.Properties["manager"].Value;
var StartIndex = Manager.IndexOf("=");
var EndIndex = Manager.IndexOf(",");
StartIndex++;
EndIndex--;
var Length = EndIndex - StartIndex;
Manager = Manager.Substring(StartIndex, Length);


The string returned in the first line of code is in this format:


  CN=Joseph Rod,OU=LaptopUser,OU=Users,DC=Company,DC=local


The goal is to just get the name and drop the rest. As the Name length will vary and my RegEX is rusty I could not come up with a working RegEX for this or any other way in C# to pull the name.

Edit:

Buy more efficient I mean faster run times and less resource utilization. This same code will be used in multiple spots in loops of up to 500 iterations (and it will continue to grow as the company does).
Here's a simple Regex to pull out the CN value:

CN=([^,]*),


You can use this C# code to get the value:

string CN = Regex.Replace(input, @"CN=([^,]*),", "$1");


Here are the important points:


I'm assuming you want everything between the "CN=" and the next comma.
The Regex replace allows a special string in the last parameter.  The "$1" will be replaced with all of the characters in between the parentheses.
You can test out the Regex here https://regex101.com/r/dT8lP8/1


In terms of computational efficiency, your solution is most likely more efficient than this Regex solution.  You might see an improvement if you replace the second through fifth lines of your solution with this:

var StartIndex = Manager.IndexOf("=") + 1;
var EndIndex = Manager.IndexOf(",", StartIndex) - 1;


I'm using this overload of the IndexOf method that tells the code at what index to start looking for the comma.  This starts the search at index 3 ("J") instead of index 0 ("C").  Again, this might run a little faster, but probably not measurably so.