Skip to content
48 changes: 25 additions & 23 deletions snippets/csharp/VS_Snippets_CLR/string.compare3/CS/comp3.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
//<snippet1>
// Sample for String.Compare(String, Int32, String, Int32, Int32)
// Sample for String.Compare(String, Int32, String, Int32, Int32)
using System;

class Sample {
public static void Main() {
// 0123456
String str1 = "machine";
String str2 = "device";
String str;
int result;
class Sample
{
public static void Main()
{
//<snippet1>
String str1 = "machine";
String str2 = "device";
String str;
int result;

Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);

/*
This example produces the following results:

str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
*/
//</snippet1>
}
}
/*
This example produces the following results:

str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
*/
//</snippet1>
68 changes: 35 additions & 33 deletions snippets/csharp/VS_Snippets_CLR/string.compare4/CS/comp4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean)
using System;

class Sample {
public static void Main() {
// 0123456
String str1 = "MACHINE";
String str2 = "machine";
String str;
int result;
class Sample
{
public static void Main()
{
String str1 = "MACHINE";
String str2 = "machine";
String str;
int result;

Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case:");
result = String.Compare(str1, 2, str2, 2, 2, true);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case:");
result = String.Compare(str1, 2, str2, 2, 2, true);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);

Console.WriteLine();
Console.WriteLine("Honor case:");
result = String.Compare(str1, 2, str2, 2, 2, false);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);
}
}
/*
This example produces the following results:
Console.WriteLine();
Console.WriteLine("Honor case:");
result = String.Compare(str1, 2, str2, 2, 2, false);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2);

/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case:
Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.
str1 = 'MACHINE', str2 = 'machine'
Ignore case:
Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.

Honor case:
Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
*/
//</snippet1>
Honor case:
Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
*/
}
}
//</snippet1>
67 changes: 35 additions & 32 deletions snippets/csharp/VS_Snippets_CLR/string.indexof1/CS/ixof1.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
//<snippet1>
// Sample for String.IndexOf(Char, Int32)
// Sample for String.IndexOf(Char, Int32)
using System;

class Sample {
public static void Main() {
class Sample
{
public static void Main()
{

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

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

at = 0;
start = 0;
while((start < str.Length) && (at > -1))
at = 0;
start = 0;
while((start < str.Length) && (at > -1))
{
at = str.IndexOf('t', start);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
at = str.IndexOf('t', start);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
Console.WriteLine();

/*
This example produces the following results:

All occurrences of 't' from position 0 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
All occurrences of 't' from position 0 to 68.
0----+----1----+----2----+----3----+----4----+----5----+----6----+---
012345678901234567890123456789012345678901234567890123456789012345678
Now is the time for all good men to come to the aid of their country.

The letter 't' occurs at position(s): 7 11 33 41 44 55 64
The letter 't' occurs at position(s): 7 11 33 41 44 55 65

*/
//</snippet1>
*/
//</snippet1>
}
}
80 changes: 41 additions & 39 deletions snippets/csharp/VS_Snippets_CLR/string.indexof8/CS/ixof8.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
//<snippet1>
// Sample for String.IndexOf(String, Int32, Int32)
// Sample for String.IndexOf(String, Int32, Int32)
using System;

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

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

end = str.Length;
start = end/2;
Console.WriteLine();
Console.WriteLine("All occurrences of 'he' from position {0} to {1}.", start, end-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("The string 'he' occurs at position(s): ");

count = 0;
at = 0;
while((start <= end) && (at > -1))
count = 0;
at = 0;
while((start <= end) && (at > -1))
{
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
// start+count must be a position within -str-.
count = end - start;
at = str.IndexOf("he", start, count);
if (at == -1) break;
Console.Write("{0} ", at);
start = at+1;
}
Console.WriteLine();
}
}
/*
This example produces the following results:
Console.WriteLine();

All occurrences of 'he' from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
/*
This example produces the following results:

The string 'he' occurs at position(s): 45 56
All occurrences of 'he' from position 34 to 68.
0----+----1----+----2----+----3----+----4----+----5----+----6----+---
012345678901234567890123456789012345678901234567890123456789012345678
Now is the time for all good men to come to the aid of their country.

*/
//</snippet1>
The string 'he' occurs at position(s): 45 56

*/
//</snippet1>
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
// <Snippet1>
using System;
using System;

public class Sample
{
public static void Main() {
public static void Main()
{
// <Snippet1>
String myString = "abc";
bool test1 = myString.Substring(2, 1).Equals("c"); // This is true.
Console.WriteLine(test1);
bool test2 = String.IsNullOrEmpty(myString.Substring(3, 0)); // This is true.
Console.WriteLine(test2);
try {
try
{
string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
Console.WriteLine(str3);
}
catch (ArgumentOutOfRangeException e) {
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine(e.Message);
}

// The example displays the following output:
// True
// True
// Index and length must refer to a location within the string.
// Parameter name: length
// </Snippet1>
}
}
// The example displays the following output:
// True
// True
// Index and length must refer to a location within the string.
// Parameter name: length
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
// <Snippet1>
using System;
using System.Globalization;

public class Example
{
public static void Main()
{
// <Snippet1>
string string1 = "brother";
string string2 = "Brother";
string relation;
Expand Down Expand Up @@ -53,6 +53,6 @@ public static void Main()
// 'brother' comes before 'Brother'.
// 'brother' is the same as 'Brother'.
// 'brother' comes after 'Brother'.
// </Snippet1>
}
}
// </Snippet1>