Skip to content

Commit e7da3ef

Browse files
authored
Update samples for StringSplitOptions.TrimEntries (dotnet#8598)
1 parent 7c60df2 commit e7da3ef

File tree

6 files changed

+403
-212
lines changed

6 files changed

+403
-212
lines changed

snippets/csharp/System/String/Split/options.cs

Lines changed: 127 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,84 +9,113 @@ public static void Main3()
99
//<snippet1>
1010
// This example demonstrates the String.Split() methods that use
1111
// the StringSplitOptions enumeration.
12-
string s1 = ",ONE,,TWO,,,THREE,,";
13-
string s2 = "[stop]" +
14-
"ONE[stop][stop]" +
15-
"TWO[stop][stop][stop]" +
16-
"THREE[stop][stop]";
12+
13+
// Example 1: Split a string delimited by characters
14+
Console.WriteLine("1) Split a string delimited by characters:\n");
15+
16+
string s1 = ",ONE,, TWO,, , THREE,,";
1717
char[] charSeparators = new char[] { ',' };
18-
string[] stringSeparators = new string[] { "[stop]" };
1918
string[] result;
20-
// ------------------------------------------------------------------------------
21-
// Split a string delimited by characters.
22-
// ------------------------------------------------------------------------------
23-
Console.WriteLine("1) Split a string delimited by characters:\n");
2419

25-
// Display the original string and delimiter characters.
26-
Console.WriteLine($"1a) The original string is \"{s1}\".");
27-
Console.WriteLine($"The delimiter character is '{charSeparators[0]}'.\n");
20+
Console.WriteLine($"The original string is: \"{s1}\".");
21+
Console.WriteLine($"The delimiter character is: '{charSeparators[0]}'.\n");
2822

29-
// Split a string delimited by characters and return all elements.
30-
Console.WriteLine("1b) Split a string delimited by characters and " +
31-
"return all elements:");
23+
// Split the string and return all elements
24+
Console.WriteLine("1a) Return all elements:");
3225
result = s1.Split(charSeparators, StringSplitOptions.None);
3326
Show(result);
3427

35-
// Split a string delimited by characters and return all non-empty elements.
36-
Console.WriteLine("1c) Split a string delimited by characters and " +
37-
"return all non-empty elements:");
28+
// Split the string and return all elements with whitespace trimmed
29+
Console.WriteLine("1b) Return all elements with whitespace trimmed:");
30+
result = s1.Split(charSeparators, StringSplitOptions.TrimEntries);
31+
Show(result);
32+
33+
// Split the string and return all non-empty elements
34+
Console.WriteLine("1c) Return all non-empty elements:");
3835
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
3936
Show(result);
4037

41-
// Split the original string into the string and empty string before the
42-
// delimiter and the remainder of the original string after the delimiter.
43-
Console.WriteLine("1d) Split a string delimited by characters and " +
44-
"return 2 elements:");
38+
// Split the string and return all non-whitespace elements with whitespace trimmed
39+
Console.WriteLine("1d) Return all non-whitespace elements with whitespace trimmed:");
40+
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
41+
Show(result);
42+
43+
44+
// Split the string into only two elements, keeping the remainder in the last match
45+
Console.WriteLine("1e) Split into only two elements:");
4546
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
4647
Show(result);
4748

48-
// Split the original string into the string after the delimiter and the
49-
// remainder of the original string after the delimiter.
50-
Console.WriteLine("1e) Split a string delimited by characters and " +
51-
"return 2 non-empty elements:");
49+
// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
50+
Console.WriteLine("1f) Split into only two elements with whitespace trimmed:");
51+
result = s1.Split(charSeparators, 2, StringSplitOptions.TrimEntries);
52+
Show(result);
53+
54+
// Split the string into only two non-empty elements, keeping the remainder in the last match
55+
Console.WriteLine("1g) Split into only two non-empty elements:");
5256
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
5357
Show(result);
5458

55-
// ------------------------------------------------------------------------------
56-
// Split a string delimited by another string.
57-
// ------------------------------------------------------------------------------
59+
// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
60+
Console.WriteLine("1h) Split into only two non-whitespace elements with whitespace trimmed:");
61+
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
62+
Show(result);
63+
64+
65+
// Example 2: Split a string delimited by another string
5866
Console.WriteLine("2) Split a string delimited by another string:\n");
5967

60-
// Display the original string and delimiter string.
61-
Console.WriteLine($"2a) The original string is \"{s2}\".");
62-
Console.WriteLine($"The delimiter string is \"{stringSeparators[0]}\".\n");
68+
string s2 = "[stop]" +
69+
"ONE[stop] [stop]" +
70+
"TWO [stop][stop] [stop]" +
71+
"THREE[stop][stop] ";
72+
string[] stringSeparators = new string[] { "[stop]" };
6373

64-
// Split a string delimited by another string and return all elements.
65-
Console.WriteLine("2b) Split a string delimited by another string and " +
66-
"return all elements:");
74+
Console.WriteLine($"The original string is: \"{s2}\".");
75+
Console.WriteLine($"The delimiter string is: \"{stringSeparators[0]}\".\n");
76+
77+
// Split the string and return all elements
78+
Console.WriteLine("2a) Return all elements:");
6779
result = s2.Split(stringSeparators, StringSplitOptions.None);
6880
Show(result);
6981

70-
// Split the original string at the delimiter and return all non-empty elements.
71-
Console.WriteLine("2c) Split a string delimited by another string and " +
72-
"return all non-empty elements:");
82+
// Split the string and return all elements with whitespace trimmed
83+
Console.WriteLine("2b) Return all elements with whitespace trimmed:");
84+
result = s2.Split(stringSeparators, StringSplitOptions.TrimEntries);
85+
Show(result);
86+
87+
// Split the string and return all non-empty elements
88+
Console.WriteLine("2c) Return all non-empty elements:");
7389
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
7490
Show(result);
7591

76-
// Split the original string into the empty string before the
77-
// delimiter and the remainder of the original string after the delimiter.
78-
Console.WriteLine("2d) Split a string delimited by another string and " +
79-
"return 2 elements:");
92+
// Split the string and return all non-whitespace elements with whitespace trimmed
93+
Console.WriteLine("2d) Return all non-whitespace elements with whitespace trimmed:");
94+
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
95+
Show(result);
96+
97+
98+
// Split the string into only two elements, keeping the remainder in the last match
99+
Console.WriteLine("2e) Split into only two elements:");
80100
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
81101
Show(result);
82102

83-
// Split the original string into the string after the delimiter and the
84-
// remainder of the original string after the delimiter.
85-
Console.WriteLine("2e) Split a string delimited by another string and " +
86-
"return 2 non-empty elements:");
103+
// Split the string into only two elements with whitespace trimmed, keeping the remainder in the last match
104+
Console.WriteLine("2f) Split into only two elements with whitespace trimmed:");
105+
result = s2.Split(stringSeparators, 2, StringSplitOptions.TrimEntries);
106+
Show(result);
107+
108+
// Split the string into only two non-empty elements, keeping the remainder in the last match
109+
Console.WriteLine("2g) Split into only two non-empty elements:");
87110
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
88111
Show(result);
89112

113+
// Split the string into only two non-whitespace elements with whitespace trimmed, keeping the remainder in the last match
114+
Console.WriteLine("2h) Split into only two non-whitespace elements with whitespace trimmed:");
115+
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
116+
Show(result);
117+
118+
90119
// Display the array of separated strings using a local function
91120
void Show(string[] entries)
92121
{
@@ -103,45 +132,77 @@ void Show(string[] entries)
103132
104133
1) Split a string delimited by characters:
105134
106-
1a) The original string is ",ONE,,TWO,,,THREE,,".
107-
The delimiter character is ','.
135+
The original string is: ",ONE,, TWO,, , THREE,,".
136+
The delimiter character is: ','.
108137
109-
1b) Split a string delimited by characters and return all elements:
138+
1a) Return all elements:
139+
The return value contains these 9 elements:
140+
<><ONE><>< TWO><>< >< THREE><><>
141+
142+
1b) Return all elements with whitespace trimmed:
110143
The return value contains these 9 elements:
111144
<><ONE><><TWO><><><THREE><><>
112145
113-
1c) Split a string delimited by characters and return all non-empty elements:
146+
1c) Return all non-empty elements:
147+
The return value contains these 4 elements:
148+
<ONE>< TWO>< >< THREE>
149+
150+
1d) Return all non-whitespace elements with whitespace trimmed:
114151
The return value contains these 3 elements:
115152
<ONE><TWO><THREE>
116153
117-
1d) Split a string delimited by characters and return 2 elements:
154+
1e) Split into only two elements:
118155
The return value contains these 2 elements:
119-
<><ONE,,TWO,,,THREE,,>
156+
<><ONE,, TWO,, , THREE,,>
120157
121-
1e) Split a string delimited by characters and return 2 non-empty elements:
158+
1f) Split into only two elements with whitespace trimmed:
122159
The return value contains these 2 elements:
123-
<ONE><TWO,,,THREE,,>
160+
<><ONE,, TWO,, , THREE,,>
161+
162+
1g) Split into only two non-empty elements:
163+
The return value contains these 2 elements:
164+
<ONE>< TWO,, , THREE,,>
165+
166+
1h) Split into only two non-whitespace elements with whitespace trimmed:
167+
The return value contains these 2 elements:
168+
<ONE><TWO,, , THREE,,>
124169
125170
2) Split a string delimited by another string:
126171
127-
2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
128-
The delimiter string is "[stop]".
172+
The original string is: "[stop]ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop] ".
173+
The delimiter string is: "[stop]".
174+
175+
2a) Return all elements:
176+
The return value contains these 9 elements:
177+
<><ONE>< ><TWO ><>< ><THREE><>< >
129178
130-
2b) Split a string delimited by another string and return all elements:
179+
2b) Return all elements with whitespace trimmed:
131180
The return value contains these 9 elements:
132181
<><ONE><><TWO><><><THREE><><>
133182
134-
2c) Split a string delimited by another string and return all non-empty elements:
183+
2c) Return all non-empty elements:
184+
The return value contains these 6 elements:
185+
<ONE>< ><TWO >< ><THREE>< >
186+
187+
2d) Return all non-whitespace elements with whitespace trimmed:
135188
The return value contains these 3 elements:
136189
<ONE><TWO><THREE>
137190
138-
2d) Split a string delimited by another string and return 2 elements:
191+
2e) Split into only two elements:
192+
The return value contains these 2 elements:
193+
<><ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop] >
194+
195+
2f) Split into only two elements with whitespace trimmed:
196+
The return value contains these 2 elements:
197+
<><ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop]>
198+
199+
2g) Split into only two non-empty elements:
139200
The return value contains these 2 elements:
140-
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
201+
<ONE>< [stop]TWO [stop][stop] [stop]THREE[stop][stop] >
141202
142-
2e) Split a string delimited by another string and return 2 non-empty elements:
203+
2h) Split into only two non-whitespace elements with whitespace trimmed:
143204
The return value contains these 2 elements:
144-
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
205+
<ONE><TWO [stop][stop] [stop]THREE[stop][stop]>
145206
146207
*/
147208
//</snippet1>
@@ -184,13 +245,13 @@ public static void Main4()
184245
// The example displays the following output:
185246
// Splitting the string:
186247
// "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
187-
//
248+
//
188249
// Using the delimiter string:
189250
// "[stop]"
190-
//
251+
//
191252
// Result including all elements (9 elements):
192253
// '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'
193-
//
254+
//
194255
// Result including non-empty elements (3 elements):
195256
// 'ONE' 'TWO' 'THREE'
196257
// </Snippet2>

0 commit comments

Comments
 (0)