0

Basically what I'm doing is reading in a large input of data to put into a structure which then goes into a generic collection. What I am needing to do is search that large amount of input for certain strings (example of such would be something like "Option=value").

"Option=value" would be a part of a larger string that I've read into memory. I want to find if "Option=value" exists and where so that I can put that value into my structure.

How should I go about doing this?

mlw4428
  • 510
  • 1
  • 6
  • 21
  • can you give sample string and your desired result? – John Woo May 02 '13 at 01:29
  • It's more of a reading in of a text file. But it would look something like `[data]` `TextColor=Red` `TextFont=Arial` and so on and so forth. – mlw4428 May 02 '13 at 01:29
  • Use `String.Contains("Option=value")` – Xaqron May 02 '13 at 01:30
  • The value I'm trying to extract is "TextColor=Red", keep in mind that what I gave is a small example, there could be 20-30 options in no particular order. @Xaqron That doesn't tell me WHERE in the string "Option=value" lives, so I cannot create a substring to import into my structure. – mlw4428 May 02 '13 at 01:32
  • there is always `[data]` on it? can you post a little real text along with your question? – Skinny Pipes May 02 '13 at 01:34
  • 2
    Are you looking for `String.IndexOf()` and `String.SubString()`? Or are you trying to access `.INI` format data? `[Section]`, followed by lines of `SomeOpt=SomeValue` pairs? If so, see [this question](http://stackoverflow.com/q/217902/62576) or [this one](http://stackoverflow.com/q/12441567/62576). – Ken White May 02 '13 at 01:34
  • 2
    You can find start point by `String.IndexOf("Option=value")` and use the result for subsequent `String.IndexOf("Option=value", previousIndex)` – Xaqron May 02 '13 at 01:35
  • It would be incredibly helpful to know what the input format is. Html? INI? What? – Sam Axe May 02 '13 at 01:37
  • Both Kevin White and Xaqron answered the question...excellent answers guys...thank you so much! – mlw4428 May 02 '13 at 01:41

1 Answers1

0

IndexOf will return >-1 for both "Option=value" and "AlternativeOption=value" or "TextColor=Red" and "CommentTextColor=Red".

Therefore it is mandatory to incorporate separation characters while using IndexOf or depending on the input layout use Regex.

ie for options in the form "[option=value]" use

Regex.Matches(input, "[TextColor=Red]");

with or without RegexOptions or

input.IndexOf("[TextColor=Red]", start);