Click here to Skip to main content
15,924,482 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I want to extract a substring from a string which the user enters into the textbox in C#. The substring I need would be in between the string entered.
For example,
if the user enters "select asd,fkdl from table",
I need "asd,fkdl" as my substring.

How do I do that Please help.

What I have tried:

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string x = (sender as Control).Text;
            string temp;
            listBox1.BeginUpdate();
            string str = textBox1.Text.ToString();
            int p = str.IndexOf(' ');
            int len = str.Length;
            try
            {
                for (int i = 8; str[i] != ' '; i++)
                {
                    var substr = str.Substring(8,i+1);
                    temp = substr;
                    MessageBox.Show(temp,"Substring");
                }  
}
            finally
            {
                listBox1.EndUpdate();
            }
Posted
Updated 20-Jul-16 8:00am
v2
Comments
Leo Chapiro 20-Jul-16 5:32am    
If the substring you are looking for is separated by blancs, you can use the Split methode of String-Class.

It is time to learn about Regular Expressions[^].
 
Share this answer
 
Use a regex:
C#
string input = "select asd,fkdl from table";
Match m = Regex.Match(input, @"(?<=select\s+).+?(?=\s+from)", RegexOptions.IgnoreCase);
if (m.Success)
    {
    string substr = m.Value;
    }
 
Share this answer
 
Comments
Ankush Soni 20-Jul-16 23:52pm    
Thank you So Much. This worked. :)
OriginalGriff 21-Jul-16 3:33am    
You're welcome!
without using Regex
C#
string input = "select asd,fkdl from table";
        int start = input.IndexOf("select")+6;
        var columns =  input.Substring(start, input.IndexOf("from") - start).Trim(); //"asd,fkdl"
 
Share this answer
 
Comments
Ankush Soni 20-Jul-16 6:20am    
Thank you so much. Just wanted to know one thing, do I still need to use the for loop?
Karthik_Mahalingam 20-Jul-16 6:25am    
no need of any loop.
Ankush Soni 20-Jul-16 6:37am    
Thank you.:)
But this worked for a specific case. the string input from the user is varying or we can say that the users enter his string in a textbox
Karthik_Mahalingam 20-Jul-16 6:55am    
you have to analyse for multiple scenario and code based on that.
Ankush Soni 20-Jul-16 23:54pm    
Thank you so much for the basic idea. Helped to develop the working code for other cases, without using regex. :)
 
Share this answer
 
Obviously you do Need some SQL parsing. This solution is maybe overkill, on the other hand it will help you in case you go in future for more complex SQL statements.

With TSql110Parser you can easely let parse an SQL. You need to add the reference for Microsoft.SqlServer.TransactSql.ScriptDom, see also link at the end of this answer.
C#
private void buttonParseSQL_Click(object sender, EventArgs e)
{
    string sql = "SELECT a, b, c FROM tbl";

    // Parser
    IList<parseerror> errors;
    TSql110Parser parser = new TSql110Parser(true);
    var sqlFragment = parser.Parse(new System.IO.StringReader(sql), out errors);

    IList<tsqlparsertoken> tokenStream= sqlFragment.ScriptTokenStream;
    foreach (TSqlParserToken token in tokenStream)
    {
        // Do what you need with TSqlParserToken
        //         public int Column { get; set; }
        //         public int Line { get; set; }
        //         public int Offset { get; set; }
        //         public string Text { get; set; }
        //         public TSqlTokenType TokenType { get; set; }
        //         public bool IsKeyword();
    }
 }

Here you will find some usefull innformation: Removing Comments from SQL | Michael J. Swart[^]Hope it helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900