[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

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?
0
johnkainn
Asked:
johnkainn
1 Solution
 
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
 
jandromedaCommented:
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now