c# - Check if string contains string from an array then use that in LastIndexOf method -
i want check if input string contains 1 of 3 strings , use later on. here's have far:
// example of 1 out of 3 possible variations string titleid = "document period ended 31 march 2014"; // array values represent 3 possible variations 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 check string array has contains method found , use 1 in lastindexof method. on right track here?
edit:
sorry confusion here. titleid.lastindexof(string) <- string dummy , represents i'd achieve here. using contains method check 1 of values f.eg. if(titleid.contains"ended ") , i'd titleid.lastindexof("ended "). have 3 separate blocks each based on "ended", "ended" or "ending" using each 1 in lastindexof method want make more simple , flexible towards input, otherwise i'd have 3 times more code , avoid that.
edit nr 2:
how achieve same result if couldn't use system.linq? cause solution provided here works when test in ide, software using code doesn't give me possibility declare "using system.linq". i'm thinking need have system.linq.enumerable.firstordefault.
// example of 1 out of 3 possible variations string titleid = "document period ended 31 march 2014"; // array values represent 3 possible variations 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)); }
Comments
Post a Comment