2

I can't seem to find a good solution to this issue. I've got an array of strings that are fed in from a report that I recieve about lost or stolen equipment. I've been using the string.IndexOf function through the rest of the form and it works quite well. This issue is with the field that says if the device was lost or stolen.

Example:

"Lost or Stolen? Lost"

"Lost or Stolen? Stolen"

I need to be able to read this but when I do string.IndexOf(@"Lost") it will always return lost because it's in the question.

Unfortunately I'm not able to change the form itself in any way and due to the nature of how it's submited I can't just write code the knocks the first 15 or so characters off the string because that may be too few in some cases.

I would really like something in C# that would allow me to continue to search a string after the first result is found so that the logic would look like:

 string my_string = "Lost or Stolen? Stolen";

 searchFor(@"Stolen" in my_string)
 {
      Found Stolen;
      Does it have "or " infront of it? yes;
      ignore and keep searching;
      Found Stolen again;
      return "Equipment stolen";
 }
oleksii
  • 33,544
  • 11
  • 83
  • 149
DarkShadow
  • 185
  • 2
  • 4
  • 14

5 Answers5

3

Couple of options here. You could look for the last index of a space and take the rest of the string:

string input = "Lost or Stolen? Stolen";
int lastSpaceIndex = input.LastIndexOf(' ');
string result = input.Substring(lastSpaceIndex + 1);
Console.WriteLine(result);

Or you could split it and take the last word:

string input = "Lost or Stolen? Lost";
string result = input.Split(' ').Last();
Console.WriteLine(result);

Regex is also an option, but overkill given the simpler solutions above. A nice shortcut that fits this scenario is to use the RegexOptions.RightToLeft option to get the first match starting from the right:

string result = Regex.Match(input, @"\w+", RegexOptions.RightToLeft).Value;
Ahmad Mageed
  • 88,056
  • 18
  • 152
  • 168
2

If I understand your requirement, you're looking for an instance of Lost or Stolen after a ?:

var q = myString.IndexOf("?");
var lost = q >= 0 && myString.IndexOf("Lost", q) > 0;
var stolen = q >= 0 && myString.IndexOf("Stolen", q) > 0;

// or
var lost = myString.LastIndexOf("Lost") > myString.IndexOf("?");
var stolen = myString.LastIndexOf("Stolen") > myString.IndexOf("?");

// don't forget
var neither = !lost && !stolen;
user7116
  • 60,025
  • 16
  • 134
  • 166
1

You can look for the string 'Lost' and if it occurs twice, then you can confirm it is 'Lost'.

user1100941
  • 101
  • 1
  • 11
0

Its possible in this case that you could use index of on a substring knowing that it is always going to say lost or stolen first

so you parse out the lost or stolen, then like for you keyword to match the remaining string.

something like:

int questionIndex = inputValue.indexOf("?");
string toMatch = inputValue.Substring(questionIndex);
if(toMatch == "Lost")
tam
  • 1,546
  • 2
  • 13
  • 25
0

If it works for your use case, it might be easier to use .EndsWith().

bool lost = my_string.EndsWith("Lost");
recursive
  • 77,417
  • 29
  • 137
  • 228