简体   繁体   中英

splitting string into array with a specific number of elements, c#

I have a string which consists number of ordered terms separated by lines (\\n) as it shown in the following example: (note, the string I have is an element of an array of string)

term 1
term 2
.......
.......
term n

I want to split a specific number of terms, let we say (1000) only and discard the rest of the terms. I'm trying the following code :

string[] training = traindocs[tr].Trim().Split('\n');
                List <string> trainterms = new List<string>();
                for (int i = 0; i < 1000; i++)
                {
                    if (i >= training.Length)
                        break;
                    trainterms.Add(training[i].Trim().Split('\t')[0]);
                } 

Can I conduct this operation without using List or any other data structure? I mean just extract the specific number of the terms into the the Array (training) directly ?? thanks in advance.

How about LINQ? The .Take() extension method kind of seems to fit your bill:

List<string> trainterms = traindocs[tr].Trim().Split('\n').Take(1000).ToList();

According to MSDN you can use an overloaded version of the split method.

public string[] Split( char[] separator, int count, StringSplitOptions options )

Parameters

separator Type: System.Char[] An array of Unicode characters that delimit the substrings in this string, an empty array that contains no delimiters, or null.

count Type: System.Int32 The maximum number of substrings to return.

options Type: System.StringSplitOptions StringSplitOptions.RemoveEmptyEntries to omit empty array elements from the array returned; or StringSplitOptions.None to include empty array elements in the array returned.

Return Value

Type: System.String[] An array whose elements contain the substrings in this string that are delimited by one or more characters in separator. For more information, see the Remarks section.

So something like so:

String str = "A,B,C,D,E,F,G,H,I";
            String[] str2 = str.Split(new Char[]{','}, 5, StringSplitOptions.RemoveEmptyEntries);
            System.Console.WriteLine(str2.Length);
            System.Console.Read();

Would print: 5

EDIT: Upon further investigation it seems that the count parameter just instructs when the splitting stops. The rest of the string will be kept in the last element.

So, the code above, would yield the following result: [0] = A, [1] = B, [2] = C, [3] = D, [4] = E,F,G,H,I , which is not something you seem to be after.

To fix this, you would need to do something like so:

String str = "A\nB\nC\nD\nE\nF\nG\nH\nI";
            List<String> myList = str.Split(new Char[]{'\n'}, 5, StringSplitOptions.RemoveEmptyEntries).ToList<String>();
            myList[myList.Count - 1] = myList[myList.Count - 1].Split(new Char[] { '\n' })[0];
            System.Console.WriteLine(myList.Count);
            foreach (String str1 in myList)
            {
                System.Console.WriteLine(str1);
            }
            System.Console.Read();

The code above will only retain the first 5 (in your case, 1000) elements. Thus, I think that Darin's solution might be cleaner, if you will.

If you want most efficient(fastest) way, you have to use overload of String.Split , passing total number of items required.

If you want easy way, use LINQ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM