Skip to content

Commit 62a7779

Browse files
WilliamAntonRohmmairaw
authored andcommitted
updating for Try .NET -- System.String.* (#1149)
* updating for Try .NET * fix indentation and moved comments * fix indentation * fix indentation * use Allman style * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * undo change + indentation * remove extra line * undo change + indentation * undo change + indentation * fix indentation + output * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * fix indentation * undo change * undo change * fix output * undo change + fix output * undo change * undo change * undo change
1 parent 6a03219 commit 62a7779

File tree

27 files changed

+513
-490
lines changed

27 files changed

+513
-490
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
//<snippet1>
2-
using System;
1+
using System;
32

43
class Example
54
{
65
public static void Main()
76
{
8-
string s1 = "The quick brown fox jumps over the lazy dog";
9-
string s2 = "fox";
10-
bool b = s1.Contains(s2);
11-
Console.WriteLine("'{0}' is in the string '{1}': {2}",
12-
s2, s1, b);
13-
if (b) {
14-
int index = s1.IndexOf(s2);
15-
if (index >= 0)
16-
Console.WriteLine("'{0} begins at character position {1}",
17-
s2, index + 1);
18-
}
7+
//<snippet1>
8+
string s1 = "The quick brown fox jumps over the lazy dog";
9+
string s2 = "fox";
10+
bool b = s1.Contains(s2);
11+
Console.WriteLine("'{0}' is in the string '{1}': {2}",
12+
s2, s1, b);
13+
if (b) {
14+
int index = s1.IndexOf(s2);
15+
if (index >= 0)
16+
Console.WriteLine("'{0} begins at character position {1}",
17+
s2, index + 1);
18+
}
19+
// This example display the following output:
20+
// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
21+
// 'fox begins at character position 17
22+
//</snippet1>
1923
}
2024
}
21-
// This example display the following output:
22-
// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
23-
// 'fox begins at character position 17
24-
//</snippet1>

snippets/csharp/VS_Snippets_CLR/string.length/CS/length.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
//<snippet1>
2-
using System;
1+
using System;
32

43
class Sample
54
{
65
public static void Main()
76
{
8-
string str = "abcdefg";
9-
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length);
10-
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length);
11-
12-
int length = str.Length;
13-
Console.WriteLine("3) The length of '{0}' is {1}", str, length);
7+
//<snippet1>
8+
string str = "abcdefg";
9+
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length);
10+
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length);
11+
12+
int length = str.Length;
13+
Console.WriteLine("3) The length of '{0}' is {1}", str, length);
14+
15+
// This example displays the following output:
16+
// 1) The length of 'abcdefg' is 7
17+
// 2) The length of 'xyz' is 3
18+
// 3) The length of 'abcdefg' is 7
19+
//</snippet1>
1420
}
1521
}
16-
// This example displays the following output:
17-
// 1) The length of 'abcdefg' is 7
18-
// 2) The length of 'xyz' is 3
19-
// 3) The length of 'abcdefg' is 7
20-
//</snippet1>

snippets/csharp/VS_Snippets_CLR_System/System.String.Contains/cs/ContainsExt1.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,43 @@ public static class StringExtensions
66
public static bool Contains(this String str, String substring,
77
StringComparison comp)
88
{
9-
if (substring == null)
10-
throw new ArgumentNullException("substring",
9+
if (substring == null)
10+
throw new ArgumentNullException("substring",
1111
"substring cannot be null.");
12-
else if (! Enum.IsDefined(typeof(StringComparison), comp))
13-
throw new ArgumentException("comp is not a member of StringComparison",
12+
else if (! Enum.IsDefined(typeof(StringComparison), comp))
13+
throw new ArgumentException("comp is not a member of StringComparison",
1414
"comp");
1515

16-
return str.IndexOf(substring, comp) >= 0;
16+
return str.IndexOf(substring, comp) >= 0;
1717
}
1818
}
1919
// </Snippet1>
2020

2121
namespace App
2222
{
23-
// <Snippet2>
2423
using System;
2524

2625
public class Example
2726
{
28-
public static void Main()
29-
{
30-
String s = "This is a string.";
31-
String sub1 = "this";
32-
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
33-
StringComparison comp = StringComparison.Ordinal;
34-
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
35-
36-
comp = StringComparison.OrdinalIgnoreCase;
37-
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
38-
}
27+
public static void Main()
28+
{
29+
// <Snippet2>
30+
String s = "This is a string.";
31+
String sub1 = "this";
32+
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
33+
StringComparison comp = StringComparison.Ordinal;
34+
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
35+
36+
comp = StringComparison.OrdinalIgnoreCase;
37+
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
38+
39+
// The example displays the following output:
40+
// Does 'This is a string.' contain 'this'?
41+
// Ordinal: False
42+
// OrdinalIgnoreCase: True
43+
// </Snippet2>
44+
}
3945
}
40-
// The example displays the following output:
41-
// Does 'This is a string.' contain 'this'?
42-
// Ordinal: False
43-
// OrdinalIgnoreCase: True
44-
// </Snippet2>
4546
}
4647

4748

snippets/csharp/VS_Snippets_CLR_System/System.String.Substring/cs/Substring1.cs

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

43
public class Example
54
{
6-
public static void Main()
7-
{
8-
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
5+
public static void Main()
6+
{
7+
// <Snippet1>
8+
String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
99
"Title=Code Repository" };
10-
foreach (var pair in pairs) {
11-
int position = pair.IndexOf("=");
12-
if (position < 0)
13-
continue;
14-
Console.WriteLine("Key: {0}, Value: '{1}'",
10+
foreach (var pair in pairs) {
11+
int position = pair.IndexOf("=");
12+
if (position < 0)
13+
continue;
14+
Console.WriteLine("Key: {0}, Value: '{1}'",
1515
pair.Substring(0, position),
1616
pair.Substring(position + 1));
17-
}
18-
}
17+
}
18+
19+
// The example displays the following output:
20+
// Key: Color1, Value: 'red'
21+
// Key: Color2, Value: 'green'
22+
// Key: Color3, Value: 'blue'
23+
// Key: Title, Value: 'Code Repository'
24+
// </Snippet1>
25+
}
1926
}
20-
// The example displays the following output:
21-
// Key: Color1, Value: 'red'
22-
// Key: Color2, Value: 'green'
23-
// Key: Color3, Value: 'blue'
24-
// Key: Title, Value: 'Code Repository'
25-
// </Snippet1>

snippets/csharp/VS_Snippets_CLR_System/System.String.Substring/cs/Substring10.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
//<snippet10>
2-
using System;
1+
using System;
32

4-
public class SubStringTest {
5-
public static void Main() {
3+
public class SubStringTest
4+
{
5+
public static void Main()
6+
{
7+
//<snippet10>
68
string [] info = { "Name: Felica Walker", "Title: Mz.",
79
"Age: 47", "Location: Paris", "Gender: F"};
810
int found = 0;
@@ -12,24 +14,26 @@ public static void Main() {
1214
Console.WriteLine(s);
1315

1416
Console.WriteLine("\nWe want to retrieve only the key information. That is:");
15-
foreach (string s in info) {
17+
foreach (string s in info)
18+
{
1619
found = s.IndexOf(": ");
1720
Console.WriteLine(" {0}", s.Substring(found + 2));
1821
}
22+
23+
// The example displays the following output:
24+
// The initial values in the array are:
25+
// Name: Felica Walker
26+
// Title: Mz.
27+
// Age: 47
28+
// Location: Paris
29+
// Gender: F
30+
//
31+
// We want to retrieve only the key information. That is:
32+
// Felica Walker
33+
// Mz.
34+
// 47
35+
// Paris
36+
// F
37+
//</snippet10>
1938
}
2039
}
21-
// The example displays the following output:
22-
// The initial values in the array are:
23-
// Name: Felica Walker
24-
// Title: Mz.
25-
// Age: 47
26-
// Location: Paris
27-
// Gender: F
28-
//
29-
// We want to retrieve only the key information. That is:
30-
// Felica Walker
31-
// Mz.
32-
// 47
33-
// Paris
34-
// F
35-
//</snippet10>

snippets/csharp/VS_Snippets_CLR_System/System.String.Substring/cs/Substring2.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
// <Snippet2>
2-
using System;
1+
using System;
32

43
public class Example
54
{
6-
public static void Main()
7-
{
8-
String s = "aaaaabbbcccccccdd";
9-
Char charRange = 'b';
10-
int startIndex = s.IndexOf(charRange);
11-
int endIndex = s.LastIndexOf(charRange);
12-
int length = endIndex - startIndex + 1;
13-
Console.WriteLine("{0}.Substring({1}, {2}) = {3}",
5+
public static void Main()
6+
{
7+
// <Snippet2>
8+
String s = "aaaaabbbcccccccdd";
9+
Char charRange = 'b';
10+
int startIndex = s.IndexOf(charRange);
11+
int endIndex = s.LastIndexOf(charRange);
12+
int length = endIndex - startIndex + 1;
13+
Console.WriteLine("{0}.Substring({1}, {2}) = {3}",
1414
s, startIndex, length,
1515
s.Substring(startIndex, length));
16-
}
16+
17+
// The example displays the following output:
18+
// aaaaabbbcccccccdd.Substring(5, 3) = bbb
19+
// </Snippet2>
20+
}
1721
}
18-
// The example displays the following output:
19-
// aaaaabbbcccccccdd.Substring(5, 3) = bbb
20-
// </Snippet2>

snippets/csharp/VS_Snippets_CLR_System/System.String.Substring/cs/Substring3.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
// <Snippet3>
2-
using System;
1+
using System;
32

43
public class Example
54
{
6-
public static void Main()
7-
{
8-
String s = "<term>extant<definition>still in existence</definition></term>";
9-
String searchString = "<definition>";
10-
int startIndex = s.IndexOf(searchString);
11-
searchString = "</" + searchString.Substring(1);
12-
int endIndex = s.IndexOf(searchString);
13-
String substring = s.Substring(startIndex, endIndex + searchString.Length - startIndex);
14-
Console.WriteLine("Original string: {0}", s);
15-
Console.WriteLine("Substring; {0}", substring);
16-
}
5+
public static void Main()
6+
{
7+
// <Snippet3>
8+
String s = "<term>extant<definition>still in existence</definition></term>";
9+
String searchString = "<definition>";
10+
int startIndex = s.IndexOf(searchString);
11+
searchString = "</" + searchString.Substring(1);
12+
int endIndex = s.IndexOf(searchString);
13+
String substring = s.Substring(startIndex, endIndex + searchString.Length - startIndex);
14+
Console.WriteLine("Original string: {0}", s);
15+
Console.WriteLine("Substring; {0}", substring);
16+
17+
// The example displays the following output:
18+
// Original string: <term>extant<definition>still in existence</definition></term>
19+
// Substring; <definition>still in existence</definition>
20+
// </Snippet3>
21+
}
1722
}
18-
// The example displays the following output:
19-
// Original string: <term>extant<definition>still in existence</definition></term>
20-
// Substring; <definition>still in existence</definition>
21-
// </Snippet3>
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// <Snippet4>
2-
using System;
1+
using System;
32

43
public class Example
54
{
6-
public static void Main()
7-
{
8-
String value = "This is a string.";
9-
int startIndex = 5;
10-
int length = 2;
11-
String substring = value.Substring(startIndex, length);
12-
Console.WriteLine(substring);
13-
}
14-
}
15-
// The example displays the following output:
16-
// is
17-
// </Snippet4>
5+
public static void Main()
6+
{
7+
// <Snippet4>
8+
String value = "This is a string.";
9+
int startIndex = 5;
10+
int length = 2;
11+
String substring = value.Substring(startIndex, length);
12+
Console.WriteLine(substring);
1813

14+
// The example displays the following output:
15+
// is
16+
// </Snippet4>
17+
}
18+
}

0 commit comments

Comments
 (0)