开发者

How to get a substring from a large string?

开发者 https://www.devze.com 2023-04-10 20:09 出处:网络
I have a large string in the form of a query, like for example: \"SELECT column_name1, column_name2, column_name3, column_name4 FROM table1 JOIN table2 ON table1.field1 = table2.field1\" (the origina

I have a large string in the form of a query, like for example: "SELECT column_name1, column_name2, column_name3, column_name4 FROM table1 JOIN table2 ON table1.field1 = table2.field1" (the original query will have about 30 column names from joins of about 6-8 tables)

Now I just wan开发者_StackOverflowt the column names from this string. How can I achieve that?

Thank you in advance.


You could use regular expressions or some ugly IndexOf stuff. Basic string and / or regex operations.

Edit: You can find find infos regarding regular expressions on http://www.regular-expressions.info/


There are a number of ways including LINQ and RegEx but here's a process that uses string's Split function. Do note that this loop assumes that the column names will be pretty monotonic and will not assume special cases such as commas or spaces in column names.

using System;
using System.Collections.Generic;

public class MyClass
{
    public static void Main()
    {
        List<string> columns = new List<string>(); // columns collection

        string sql = "SELECT column_name1, column_name2, column_name3, column_name4 FROM table1 JOIN table2 ON table1.field1 = table2.field1";
        string[] parts = sql.Split(new char[] {' ', ','}, StringSplitOptions.RemoveEmptyEntries);

        for (int i=1; i<parts.Length; i++)
        {
            if (parts[i].Equals("FROM")) break;

            columns.Add(parts[i]); // add cols to collection
        }

        foreach(string column in columns)
            Console.WriteLine(column); // print out the columns
    }
}


You could use a regular expression - something like:

string resultString = null;
try {
resultString = Regex.Match(subjectString, "SELECT (.+) FROM", RegexOptions.Singleline  | RegexOptions.Multiline).Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}


Yikes. Rough crowd.

        string query = "SELECT this, that, there, when, who, what, FROM Words";
        string[] split = query.Split(' ');
        string[] columns = new string[30];

        for (int i = 1; i < split.Length; i++)
        {
            if (split[i].ToUpper() == "FROM")
                break;
            columns[i - 1] = split[i];
        }


I think perhaps you're going about this in the wrong way.

If you're writing this SQL string yourself, then you already know the names, and should be constructing your SQL string from some kind of collection.

If you're not writing the string, then why not get the column names from the result set? This presumes of course they have them (and aliases), and that it doesn't matter that you get the names AFTER the query is executed.

If you need to do this and you are writing this string yourself, then you need to dynamically construct the query so that you have some semblance of an idea when the data comes back out again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号