0

I want to cut the end of the sring:

Input string:

/O=Shore Tel/OU=CANDY/cn=Recipients/cn=PAgricola
/O=Shore Tel/OU=CANDY/cn=Recipients/cn=YAchmanov
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Mukul Agrawal2f2
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Nick Aiello816

Output string:

/O=Shore Tel/OU=CANDY/cn=Recipients
/O=Shore Tel/OU=CANDY/cn=Recipients
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients
/o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients

Could you give me a suggestions?

2
  • 1
    Search about String.Split, String.LastIndexOf, String.Substring. This is all you need to know to solve the puzzle. Commented Jul 26, 2014 at 11:01
  • my problem and the topic are different. I saw it Commented Jul 26, 2014 at 11:02

3 Answers 3

0

It looks like you can find the last index of "/cn=", and substring from zero to that position:

int pos = str.LastIndexOf("/cn=");
if (pos > 0) {
    str = str.Substring(0, pos);
}

Demo.

Sign up to request clarification or add additional context in comments.

Comments

0

It just one line in LINQ

string input = @"/O=Shore Tel/OU=CANDY/cn=Recipients/cn=PAgricola
                 /O=Shore Tel/OU=CANDY/cn=Recipients/cn=YAchmanov
                 /o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Mukul Agrawal2f2
                 /o=Shore Tel/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=Nick Aiello816";

var result = string.Join("\n", input.Split('\n')
                                    .Select(x => x.Substring(0, x.LastIndexOf('/')))
                                    .ToArray());
Console.WriteLine(result);

For readability I have used the verbatim @ character and aligned the substrings, but this introduces some unwanted whitespaces. It is not clear how your input string is formatted, but the key point here is the line that sets the result variable.

Comments

0
    string s = "/O=Shore Tel/OU=CANDY/cn=Recipients/cn=PAgricola";
    s=s.Substring(0, s.IndexOf("s/cn="));

2 Comments

This will cut the string at the first occurence of /cn= and it is not what has been asked.
@ steve : see the updates

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.