Search in string for all words in quotation marks

Posted on 2008-11-12
Last Modified: 2012-06-27
How is best to collect all words in a string that are in quotation marks ("...") in an array?
Question by:johnkainn
    LVL 3

    Expert Comment

    by:Rupesh P
    if you have a seperator charector like comma, you can use Split method
    Eg: string a ="aaa,bbb,cccc";
      string[] stringArray = a.split(",");.

    Author Comment

    What I had in mind was like this:
    Search string:  One, "two", three, "four", "five", six.
    Words collected in Array: two, four, five.
    LVL 12

    Accepted Solution

    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


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now