Share via


getting string between quotation marks

Question

Wednesday, October 26, 2016 12:56 PM

lets say i have a text-box and the string in it is something like 'hello this is "world" something else'

how do i get only the text between the quotation marks 

wait what

All replies (6)

Wednesday, October 26, 2016 1:06 PM âś…Answered

You could use a regular expression:

string test = "hello this is \"world\" something else";
Regex regex = new Regex("\"(.*?)\"");

var matches = regex.Matches(test);

if (matches.Count > 0)
{
    Console.WriteLine(matches[0].Groups[1]);
}

Console.ReadKey();

Here, I'm just taking the first match (matches[0]), but if you had multiple sets of text in quotation marks, you would get more matches in the collection and could cycle through all of them with a foreach

string test = "hello this is \"world\" something else";
Regex regex = new Regex("\"(.*?)\"");

var matches = regex.Matches(test);

foreach(Match match in matches)
{
    Console.WriteLine(match.Groups[1]);
}

Console.ReadKey();

Wednesday, October 26, 2016 2:06 PM

Besides regular expressions you could also use a SubString:

var text = "'hello this is \"world\" something else'";
var start = text.IndexOf("\"") + 1;//add one to not include quote
var end = text.LastIndexOf("\"") - start;
var result = text.Substring(start, end);

This is just an example, if there were more than one set of quotations in your string you would want to change this to get the next index of the quote instead of the last index.


Thursday, October 27, 2016 12:53 PM

regex does not work do i need to be using somthing

wait what


Thursday, October 27, 2016 1:28 PM

Can you please expand on "does not work".

Is it not compiling?

Do you get an error when you run it?


Thursday, October 27, 2016 4:15 PM

Hi shayVSgaming,

Thank you for posting here.

For you question, please do the following steps.

In designer.

Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace get_string_between_quotation_marks
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string val = textBox1.Text;

            Match match = Regex.Match(val, "\"([^\"]*)\""); 
            if (match.Success)
            {
                string yourValue = match.Groups[1].Value;
                textBox2.Text = yourValue;
            }
        }
    }
}

Here is the output. I input the 'hello this is "world" something else'.

I hope this would be helpful to you.

If you have something else, please feel free to contact us.

Best Regards,

Wendy

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 [email protected].


Thursday, May 2, 2019 8:25 PM

good answer