Search code examples
c#asp.netwebformstextbox

C# TextBox to use Quotations to mean whole phrase


In a Web Forms project, I have a textbox on my page that when the user enters text it automatically strips the whitespace, adds a comma, and then pushes each word into an array. (i do a bit of string.Split, string.Replace and string.ToArray)

This functionality is what i need, however i need to perform another.

When a user enters words but surrounds them with quotation marks i need that to act as a single phrase.

So if a user enters -red oak tree- this is created as an array of 3 (red and oak and tree) but if a user enters -"red oak tree"- this is seen as the phrase "red oak tree"

I do use a checkbox to handle this at the moment but i need the check to be done if the words are in quotation marks, with no checkbox.


Solution

  • Check the first and last characters

    string str = "red oak tree";
    if (str.Length > 2 && str[0] == '\"' && str[str.Length - 1] == '\"') {
        ...
    }