Skip to content

Commit f176250

Browse files
WilliamAntonRohmmairaw
authored andcommitted
updating System.String examples for Try .NET (#1192)
* updating System.String examples for Try .NET * updating System.String examples for Try .NET * updating System.String examples for Try .NET * updating System.String examples for Try .NET * roll back change * code style + indentation * code style + indentation * undo interactive * revert line * typo + indentation + code style * typo + indentation + code style * code style
1 parent 0c5dcc4 commit f176250

File tree

6 files changed

+154
-141
lines changed
  • snippets/csharp

6 files changed

+154
-141
lines changed

snippets/csharp/VS_Snippets_CLR/string.compare3/CS/comp3.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
//<snippet1>
2-
// Sample for String.Compare(String, Int32, String, Int32, Int32)
1+
// Sample for String.Compare(String, Int32, String, Int32, Int32)
32
using System;
43

5-
class Sample {
6-
public static void Main() {
7-
// 0123456
8-
String str1 = "machine";
9-
String str2 = "device";
10-
String str;
11-
int result;
4+
class Sample
5+
{
6+
public static void Main()
7+
{
8+
//<snippet1>
9+
String str1 = "machine";
10+
String str2 = "device";
11+
String str;
12+
int result;
1213

13-
Console.WriteLine();
14-
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
15-
result = String.Compare(str1, 2, str2, 0, 2);
16-
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
17-
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
18-
Console.Write("{0} ", str);
19-
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
14+
Console.WriteLine();
15+
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
16+
result = String.Compare(str1, 2, str2, 0, 2);
17+
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
18+
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
19+
Console.Write("{0} ", str);
20+
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
21+
22+
/*
23+
This example produces the following results:
24+
25+
str1 = 'machine', str2 = 'device'
26+
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
27+
*/
28+
//</snippet1>
2029
}
2130
}
22-
/*
23-
This example produces the following results:
24-
25-
str1 = 'machine', str2 = 'device'
26-
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
27-
*/
28-
//</snippet1>

snippets/csharp/VS_Snippets_CLR/string.compare4/CS/comp4.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,42 @@
22
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean)
33
using System;
44

5-
class Sample {
6-
public static void Main() {
7-
// 0123456
8-
String str1 = "MACHINE";
9-
String str2 = "machine";
10-
String str;
11-
int result;
5+
class Sample
6+
{
7+
public static void Main()
8+
{
9+
String str1 = "MACHINE";
10+
String str2 = "machine";
11+
String str;
12+
int result;
1213

13-
Console.WriteLine();
14-
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
15-
Console.WriteLine("Ignore case:");
16-
result = String.Compare(str1, 2, str2, 2, 2, true);
17-
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
18-
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
19-
Console.Write("{0} ", str);
20-
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
14+
Console.WriteLine();
15+
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
16+
Console.WriteLine("Ignore case:");
17+
result = String.Compare(str1, 2, str2, 2, 2, true);
18+
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
19+
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
20+
Console.Write("{0} ", str);
21+
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
2122

22-
Console.WriteLine();
23-
Console.WriteLine("Honor case:");
24-
result = String.Compare(str1, 2, str2, 2, 2, false);
25-
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
26-
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
27-
Console.Write("{0} ", str);
28-
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
29-
}
30-
}
31-
/*
32-
This example produces the following results:
23+
Console.WriteLine();
24+
Console.WriteLine("Honor case:");
25+
result = String.Compare(str1, 2, str2, 2, 2, false);
26+
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
27+
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
28+
Console.Write("{0} ", str);
29+
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
30+
31+
/*
32+
This example produces the following results:
3333
34-
str1 = 'MACHINE', str2 = 'machine'
35-
Ignore case:
36-
Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.
34+
str1 = 'MACHINE', str2 = 'machine'
35+
Ignore case:
36+
Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.
3737
38-
Honor case:
39-
Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
40-
*/
41-
//</snippet1>
38+
Honor case:
39+
Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
40+
*/
41+
}
42+
}
43+
//</snippet1>
Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,45 @@
1-
//<snippet1>
2-
// Sample for String.IndexOf(Char, Int32)
1+
// Sample for String.IndexOf(Char, Int32)
32
using System;
43

5-
class Sample {
6-
public static void Main() {
4+
class Sample
5+
{
6+
public static void Main()
7+
{
78

8-
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
9-
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
10-
string str = "Now is the time for all good men to come to the aid of their party.";
11-
int start;
12-
int at;
9+
//<snippet1>
10+
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+---";
11+
string br2 = "012345678901234567890123456789012345678901234567890123456789012345678";
12+
string str = "Now is the time for all good men to come to the aid of their country.";
13+
int start;
14+
int at;
1315

14-
Console.WriteLine();
15-
Console.WriteLine("All occurrences of 't' from position 0 to {0}.", str.Length-1);
16-
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
17-
Console.Write("The letter 't' occurs at position(s): ");
16+
Console.WriteLine();
17+
Console.WriteLine("All occurrences of 't' from position 0 to {0}.", str.Length-1);
18+
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
19+
Console.Write("The letter 't' occurs at position(s): ");
1820

19-
at = 0;
20-
start = 0;
21-
while((start < str.Length) && (at > -1))
21+
at = 0;
22+
start = 0;
23+
while((start < str.Length) && (at > -1))
2224
{
23-
at = str.IndexOf('t', start);
24-
if (at == -1) break;
25-
Console.Write("{0} ", at);
26-
start = at+1;
25+
at = str.IndexOf('t', start);
26+
if (at == -1) break;
27+
Console.Write("{0} ", at);
28+
start = at+1;
2729
}
28-
Console.WriteLine();
29-
}
30-
}
31-
/*
32-
This example produces the following results:
30+
Console.WriteLine();
31+
32+
/*
33+
This example produces the following results:
3334
34-
All occurrences of 't' from position 0 to 66.
35-
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
36-
0123456789012345678901234567890123456789012345678901234567890123456
37-
Now is the time for all good men to come to the aid of their party.
35+
All occurrences of 't' from position 0 to 68.
36+
0----+----1----+----2----+----3----+----4----+----5----+----6----+---
37+
012345678901234567890123456789012345678901234567890123456789012345678
38+
Now is the time for all good men to come to the aid of their country.
3839
39-
The letter 't' occurs at position(s): 7 11 33 41 44 55 64
40+
The letter 't' occurs at position(s): 7 11 33 41 44 55 65
4041
41-
*/
42-
//</snippet1>
42+
*/
43+
//</snippet1>
44+
}
45+
}
Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
1-
//<snippet1>
2-
// Sample for String.IndexOf(String, Int32, Int32)
1+
// Sample for String.IndexOf(String, Int32, Int32)
32
using System;
43

5-
class Sample {
6-
public static void Main() {
4+
class Sample
5+
{
6+
public static void Main()
7+
{
8+
//<snippet1>
9+
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+---";
10+
string br2 = "012345678901234567890123456789012345678901234567890123456789012345678";
11+
string str = "Now is the time for all good men to come to the aid of their country.";
12+
int start;
13+
int at;
14+
int end;
15+
int count;
716

8-
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
9-
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
10-
string str = "Now is the time for all good men to come to the aid of their party.";
11-
int start;
12-
int at;
13-
int end;
14-
int count;
17+
end = str.Length;
18+
start = end/2;
19+
Console.WriteLine();
20+
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end-1);
21+
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
22+
Console.Write("The string 'he' occurs at position(s): ");
1523

16-
end = str.Length;
17-
start = end/2;
18-
Console.WriteLine();
19-
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end-1);
20-
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
21-
Console.Write("The string 'he' occurs at position(s): ");
22-
23-
count = 0;
24-
at = 0;
25-
while((start <= end) && (at > -1))
24+
count = 0;
25+
at = 0;
26+
while((start <= end) && (at > -1))
2627
{
27-
// start+count must be a position within -str-.
28-
count = end - start;
29-
at = str.IndexOf("he", start, count);
30-
if (at == -1) break;
31-
Console.Write("{0} ", at);
32-
start = at+1;
28+
// start+count must be a position within -str-.
29+
count = end - start;
30+
at = str.IndexOf("he", start, count);
31+
if (at == -1) break;
32+
Console.Write("{0} ", at);
33+
start = at+1;
3334
}
34-
Console.WriteLine();
35-
}
36-
}
37-
/*
38-
This example produces the following results:
35+
Console.WriteLine();
3936

40-
All occurrences of 'he' from position 33 to 66.
41-
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
42-
0123456789012345678901234567890123456789012345678901234567890123456
43-
Now is the time for all good men to come to the aid of their party.
37+
/*
38+
This example produces the following results:
4439
45-
The string 'he' occurs at position(s): 45 56
40+
All occurrences of 'he' from position 34 to 68.
41+
0----+----1----+----2----+----3----+----4----+----5----+----6----+---
42+
012345678901234567890123456789012345678901234567890123456789012345678
43+
Now is the time for all good men to come to the aid of their country.
4644
47-
*/
48-
//</snippet1>
45+
The string 'he' occurs at position(s): 45 56
46+
47+
*/
48+
//</snippet1>
49+
}
50+
}

snippets/csharp/VS_Snippets_CLR_Classic/classic String.Substring1 Example/CS/source.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
// <Snippet1>
2-
using System;
1+
using System;
32

43
public class Sample
54
{
6-
public static void Main() {
5+
public static void Main()
6+
{
7+
// <Snippet1>
78
String myString = "abc";
89
bool test1 = myString.Substring(2, 1).Equals("c"); // This is true.
910
Console.WriteLine(test1);
1011
bool test2 = String.IsNullOrEmpty(myString.Substring(3, 0)); // This is true.
1112
Console.WriteLine(test2);
12-
try {
13+
try
14+
{
1315
string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
1416
Console.WriteLine(str3);
1517
}
16-
catch (ArgumentOutOfRangeException e) {
18+
catch (ArgumentOutOfRangeException e)
19+
{
1720
Console.WriteLine(e.Message);
1821
}
22+
23+
// The example displays the following output:
24+
// True
25+
// True
26+
// Index and length must refer to a location within the string.
27+
// Parameter name: length
28+
// </Snippet1>
1929
}
2030
}
21-
// The example displays the following output:
22-
// True
23-
// True
24-
// Index and length must refer to a location within the string.
25-
// Parameter name: length
26-
// </Snippet1>

snippets/csharp/VS_Snippets_CLR_System/system.String.Compare/cs/Example.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
1+
// <Snippet1>
2+
using System;
23
using System.Globalization;
34

45
public class Example
56
{
67
public static void Main()
78
{
8-
// <Snippet1>
99
string string1 = "brother";
1010
string string2 = "Brother";
1111
string relation;
@@ -53,6 +53,6 @@ public static void Main()
5353
// 'brother' comes before 'Brother'.
5454
// 'brother' is the same as 'Brother'.
5555
// 'brother' comes after 'Brother'.
56-
// </Snippet1>
5756
}
5857
}
58+
// </Snippet1>

0 commit comments

Comments
 (0)