Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

c# - Split string into list of string

I have a string which is 28000 lines long and is laid out like below:

List<string> sQuerys = new List<string>();

string FullFileQuery = " EXEC spDataCache_INS_XSCDV1P @Company = 'UKC                                ', @Country = 'AE                                 ', @Trade_Lane = 'ARABIAN GULF/MIDDLE EAST           ', @Trade_Region = 'INDIA/PAKISTAN/MIDDLE EAST         '
EXEC spDataCache_INS_XSCDV1P @Company = 'UKC                                ', @Country = 'AL                                 ', @Trade_Lane = 'MEDITERRANEAN                      ', @Trade_Region = 'EUROPE/MEDITERRANEAN               '
EXEC spDataCache_INS_XSCDV1P @Company = 'UKC                                ', @Country = 'AO                                 ', @Trade_Lane = 'WEST AFRICA                        ', @Trade_Region = 'AFRICA                             '
EXEC spDataCache_INS_XSCDV1P @Company = 'UKC                                ', @Country = 'AR                                 ', @Trade_Lane = 'EAST COAST SOUTH AMERICA           ', @Trade_Region = 'LATIN AMERICA                      '
EXEC spDataCache_INS_XSCDV1P @Company = 'UKC                                ', @Country = 'AU                                 ', @Trade_Lane = 'AUSTRALIA/NEW ZEALAND              ', @Trade_Region = 'FAR EAST AND OCEANIA               '"

I want to split the string every 15000th line and add to my list of string sQuerys.

So the 28000 lines will be split into 15000 lines and 13000 lines and added to the list. I am unsure on the quickest way to achieve this.

EDIT:

The code I have tried doing but I am stuck is below:

if (FullFileQuery.Split('
').Length > 15000)
{
    //28000
    int numLines = FullFileQuery.Split('
').Length;
    //LOOP TWICE.
    for (int i = 0; i < ((numLines / 15000) + 1); i++)
    {
        //NEED TO ADD TO sQuerys in here.
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use something like

class Program
{
    static void Main(string[] args)
    {
        var input = @"your long multiline string...";

        var list = new List<string>();
        var lines = input.Split('
');
        var index = 0;
        var batchSize = 15000;
        while (index < lines.Count())
        {
            list.Add(string.Join(string.Empty, lines.Skip(index).Take(batchSize)));
            index += batchSize;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...