1

I want to check if my input string contains one of 3 strings and use it later on. Here's what I have so far:

// this is just an example of 1 out of 3 possible variations
string titleID = "document for the period ended 31 March 2014";

// the array values represent 3 possible variations that I might encounter
string s1 = "ended ";
string s2 = "Ended ";
string s3 = "Ending ";
string[] sArray = new [] { s1, s2, s3};

if(sArray.Any(titleID.Contains))
{
      TakeEndPeriod = titleID.Substring(titleID.LastIndexOf(string));
}

I want to check which string from the array has the Contains method found and use exactly that one in the LastIndexOf method. Am I on the right track here?

EDIT:

Sorry for any confusion here. titleID.LastIndexOf(string) <- the string is just a dummy and it represents what I'd like to achieve here. I was previously using the Contains method to check only for 1 of the values f.eg. if(titleID.Contains"ended ") and then I'd do titleID.LastIndexOf("ended "). I could have 3 separate blocks each based on "ended", "Ended" or "Ending" using each one in the LastIndexOf method but I want to make it more simple and flexible towards the input, otherwise I'd have 3 times more code and I would like to avoid that.

EDIT NR 2:

How would I achieve the same result if I couldn't use System.Linq? Cause the solution provided here works when I test it in the IDE, but the software itself which will be using this code doesn't give me the possibility declare "using System.Linq". I'm thinking I need to have something like System.Linq.Enumerable.FirstOrDefault.

3
  • Does this even compile? Commented Jun 26, 2014 at 7:33
  • You need to loop on the sArray array, checking each string against the title to see which one is found. Commented Jun 26, 2014 at 7:34
  • @user2310920 you can remove comments if you want - to the right of your own comments there's a small delete button (you'll see it if you hover your comment) Commented Jun 26, 2014 at 8:43

2 Answers 2

3
        // this is just an example of 1 out of 3 possible variations
        string titleID = "document for the period ended 31 March 2014";

        // the array values represent 3 possible variations that I might encounter
        string s1 = "ended ";
        string s2 = "Ended ";
        string s3 = "Ending ";
        string[] sArray = new [] { s1, s2, s3};

        var stringMatch = sArray.FirstOrDefault(titleID.Contains);
        if (stringMatch != null)
        {
            TakeEndPeriod = titleID.Substring(titleID.LastIndexOf(stringMatch));
        }
Sign up to request clarification or add additional context in comments.

4 Comments

+1 As a note for the OP, if none are found then valForLater will be null (in this circumstance)
And last item in the text. it can be one of the string in the array
He wants the string to pass into LastIndexOf not the index in the array
@StuartCampbell that's exactly what I want to achieve here. The idea is that after I do that I'm parsing the date into a custom format, so it's important I get the position after one of the strings from the array. UPDATE! It works! Thx!!!
0

this should do it.

// this is just an example of 1 out of 3 possible variations
string titleID = "document for the period ended 31 March 2014";

string s1 = "ended ";
string s2 = "Ended ";
string s3 = "Ending ";
string[] sArray = new [] { s1, s2, s3};

var maxLastIndex = -2; 
foreach(var s in sArray)
{
    var lastIndex = titleID.LastIndexOf(s);
    if(lastIndex > maxLastIndex)
        maxLastIndex = lastIndex;
}

/// if maxLastIndex is still -1 it means no matching elements exist in the string.

2 Comments

maxLastIndex can never finish as -2 since LastIndexOf will return -1
There is no loop that needs to be ended with that -1 constraint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.