@@ -9,84 +9,113 @@ public static void Main3()
9
9
//<snippet1>
10
10
// This example demonstrates the String.Split() methods that use
11
11
// 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,, ";
17
17
char [ ] charSeparators = new char [ ] { ',' } ;
18
- string [ ] stringSeparators = new string [ ] { "[stop]" } ;
19
18
string [ ] result ;
20
- // ------------------------------------------------------------------------------
21
- // Split a string delimited by characters.
22
- // ------------------------------------------------------------------------------
23
- Console . WriteLine ( "1) Split a string delimited by characters:\n " ) ;
24
19
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 ") ;
28
22
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:" ) ;
32
25
result = s1 . Split ( charSeparators , StringSplitOptions . None ) ;
33
26
Show ( result ) ;
34
27
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:" ) ;
38
35
result = s1 . Split ( charSeparators , StringSplitOptions . RemoveEmptyEntries ) ;
39
36
Show ( result ) ;
40
37
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:" ) ;
45
46
result = s1 . Split ( charSeparators , 2 , StringSplitOptions . None ) ;
46
47
Show ( result ) ;
47
48
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:" ) ;
52
56
result = s1 . Split ( charSeparators , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
53
57
Show ( result ) ;
54
58
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
58
66
Console . WriteLine ( "2) Split a string delimited by another string:\n " ) ;
59
67
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]" } ;
63
73
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:" ) ;
67
79
result = s2 . Split ( stringSeparators , StringSplitOptions . None ) ;
68
80
Show ( result ) ;
69
81
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:" ) ;
73
89
result = s2 . Split ( stringSeparators , StringSplitOptions . RemoveEmptyEntries ) ;
74
90
Show ( result ) ;
75
91
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:" ) ;
80
100
result = s2 . Split ( stringSeparators , 2 , StringSplitOptions . None ) ;
81
101
Show ( result ) ;
82
102
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:" ) ;
87
110
result = s2 . Split ( stringSeparators , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
88
111
Show ( result ) ;
89
112
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
+
90
119
// Display the array of separated strings using a local function
91
120
void Show ( string [ ] entries )
92
121
{
@@ -103,45 +132,77 @@ void Show(string[] entries)
103
132
104
133
1) Split a string delimited by characters:
105
134
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: ','.
108
137
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:
110
143
The return value contains these 9 elements:
111
144
<><ONE><><TWO><><><THREE><><>
112
145
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:
114
151
The return value contains these 3 elements:
115
152
<ONE><TWO><THREE>
116
153
117
- 1d ) Split a string delimited by characters and return 2 elements:
154
+ 1e ) Split into only two elements:
118
155
The return value contains these 2 elements:
119
- <><ONE,,TWO,,, THREE,,>
156
+ <><ONE,, TWO,, , THREE,,>
120
157
121
- 1e ) Split a string delimited by characters and return 2 non-empty elements :
158
+ 1f ) Split into only two elements with whitespace trimmed :
122
159
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,,>
124
169
125
170
2) Split a string delimited by another string:
126
171
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><>< >
129
178
130
- 2b) Split a string delimited by another string and return all elements :
179
+ 2b) Return all elements with whitespace trimmed :
131
180
The return value contains these 9 elements:
132
181
<><ONE><><TWO><><><THREE><><>
133
182
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:
135
188
The return value contains these 3 elements:
136
189
<ONE><TWO><THREE>
137
190
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:
139
200
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] >
141
202
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 :
143
204
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]>
145
206
146
207
*/
147
208
//</snippet1>
@@ -184,13 +245,13 @@ public static void Main4()
184
245
// The example displays the following output:
185
246
// Splitting the string:
186
247
// "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
187
- //
248
+ //
188
249
// Using the delimiter string:
189
250
// "[stop]"
190
- //
251
+ //
191
252
// Result including all elements (9 elements):
192
253
// '<>' 'ONE' '<>' 'TWO' '<>' '<>' 'THREE' '<>' '<>'
193
- //
254
+ //
194
255
// Result including non-empty elements (3 elements):
195
256
// 'ONE' 'TWO' 'THREE'
196
257
// </Snippet2>
0 commit comments