none
How to convert words in quatation marks to string? RRS feed

  • Question

  • I have a textbox and a button. The user can write anyting he wants in the textbox. When he presses on the button, it should search for words in quataion marks and convert it to string. So let's say the following text is in a textbox:

    { Hello my name is "Billy", what's your name? }

    string should be "Billy" because "Billy" is writting between quatation marks. --> Question 1

    Also, how can I do this if there are multiple quatation marks. For example:

    { My bestfriends are "Micheal", "Henry" and "Jeff". }

    strings name1 should be "Micheal, name2 should be "Henry" and name3 should be "Jeff". --> Question 2

    There is no limit for writing words in quatation marks. The user can write as many as they want and the program should create as much strings as needed.

    Anyone has any idea how to do this?

    Tuesday, October 31, 2017 6:50 PM

All replies

  • Check this idea:

    string example = "My best friends are \"Michael\", \"Henry\" and \"Jeff\".";
    string[] found_strings = Regex.Matches( example, @"\""(.+?)\""" ).Cast<Match>().Select( m => m.Groups[1].Value ).ToArray();

     

    The found strings are stored in an array.



    • Edited by Viorel_MVP Tuesday, October 31, 2017 7:51 PM
    Tuesday, October 31, 2017 7:50 PM
  • Hello emronaldo,

    Try the below code

    private void button1_Click(object sender, EventArgs e)
            {
                string input = textBox1.Text;
                string pattern = "\"[\\w ]*\"";
                Regex r = new Regex(pattern);
                MatchCollection mc = r.Matches(input);           
                foreach (Match m in mc) {
                    Console.WriteLine(m.Value.Substring(1, m.Value.Length - 2));
    
                }
            }

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 1, 2017 7:42 AM
    Moderator