简体   繁体   中英

C# - Searching strings

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";
 }

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;

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;

您可以查找字符串“ Lost”,如果它出现两次,则可以确认它是“ Lost”。

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")

如果它适合您的用例,则使用.EndsWith()可能会更容易。

bool lost = my_string.EndsWith("Lost");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM