Search in string for all words in quotation marks

How is best to collect all words in a string that are in quotation marks ("...") in an array?
johnkainnAsked:
Who is Participating?
 
jandromedaConnect With a Mentor Commented:
Try this code. I have not test it much it seems to work fine.
string originalString = "this\"word\"\"is\"not what\"we\" need\"but\"this";
			int firstQuoteIndex = 0;
			int lastQuoteIndex = originalString.Length;
			List<string> foundWords = new List<string>();
			
			while (lastQuoteIndex > 0)
			{
				firstQuoteIndex = originalString.IndexOf('"');
				originalString = originalString.Substring(firstQuoteIndex + 1);
				firstQuoteIndex = 0;
				firstQuoteIndex = originalString.IndexOf('"');
				foundWords.Add(originalString.Substring(0, firstQuoteIndex));
				firstQuoteIndex = originalString.IndexOf('"');
				originalString = originalString.Substring(firstQuoteIndex + 1);
				lastQuoteIndex = originalString.LastIndexOf('"');
			}

Open in new window

0
 
Rupesh PCommented:
if you have a seperator charector like comma, you can use Split method
Eg: string a ="aaa,bbb,cccc";
  string[] stringArray = a.split(",");.
0
 
johnkainnAuthor Commented:
What I had in mind was like this:
Search string:  One, "two", three, "four", "five", six.
Words collected in Array: two, four, five.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.