Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>

</Project>
5 changes: 3 additions & 2 deletions snippets/csharp/System.Text/Encoding/Overview/convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System;
using System.Text;

class Example
class ConvertExample
{
static void Main()
static void Run()
{
string unicodeString = "This string contains the unicode character Pi (\u03a0)";

Expand All @@ -28,6 +28,7 @@ static void Main()
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}

// The example displays the following output:
// Original string: This string contains the unicode character Pi (Π)
// Ascii converted string: This string contains the unicode character Pi (?)
Expand Down
91 changes: 91 additions & 0 deletions snippets/csharp/System.Text/Encoding/Overview/getencoding1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// <Snippet1>
using System;
using System.Text;

public class GetEncodingExample
{
public static void Run()
{
Encoding enc = Encoding.GetEncoding(1253);
Encoding altEnc = Encoding.GetEncoding("windows-1253");
Console.WriteLine($"{enc.EncodingName} = Code Page {altEnc.CodePage}: {enc.Equals(altEnc)}");
string greekAlphabet = "Α α Β β Γ γ Δ δ Ε ε Ζ ζ Η η " +
"Θ θ Ι ι Κ κ Λ λ Μ μ Ν ν Ξ ξ " +
"Ο ο Π π Ρ ρ Σ σ ς Τ τ Υ υ " +
"Φ φ Χ χ Ψ ψ Ω ω";
Console.OutputEncoding = Encoding.UTF8;
byte[] bytes = enc.GetBytes(greekAlphabet);
Console.WriteLine("{0,-12} {1,20} {2,20:X2}", "Character",
"Unicode Code Point", "Code Page 1253");
for (int ctr = 0; ctr < bytes.Length; ctr++) {
if (greekAlphabet[ctr].Equals(' '))
continue;

Console.WriteLine("{0,-12} {1,20} {2,20:X2}", greekAlphabet[ctr],
GetCodePoint(greekAlphabet[ctr]), bytes[ctr]);
}
}

private static string GetCodePoint(char ch)
{
string retVal = "u+";
byte[] bytes = Encoding.Unicode.GetBytes(ch.ToString());
for (int ctr = bytes.Length - 1; ctr >= 0; ctr--)
retVal += bytes[ctr].ToString("X2");

return retVal;
}
}

// The example displays the following output:
// Character Unicode Code Point Code Page 1253
// Α u+0391 C1
// α u+03B1 E1
// Β u+0392 C2
// β u+03B2 E2
// Γ u+0393 C3
// γ u+03B3 E3
// Δ u+0394 C4
// δ u+03B4 E4
// Ε u+0395 C5
// ε u+03B5 E5
// Ζ u+0396 C6
// ζ u+03B6 E6
// Η u+0397 C7
// η u+03B7 E7
// Θ u+0398 C8
// θ u+03B8 E8
// Ι u+0399 C9
// ι u+03B9 E9
// Κ u+039A CA
// κ u+03BA EA
// Λ u+039B CB
// λ u+03BB EB
// Μ u+039C CC
// μ u+03BC EC
// Ν u+039D CD
// ν u+03BD ED
// Ξ u+039E CE
// ξ u+03BE EE
// Ο u+039F CF
// ο u+03BF EF
// Π u+03A0 D0
// π u+03C0 F0
// Ρ u+03A1 D1
// ρ u+03C1 F1
// Σ u+03A3 D3
// σ u+03C3 F3
// ς u+03C2 F2
// Τ u+03A4 D4
// τ u+03C4 F4
// Υ u+03A5 D5
// υ u+03C5 F5
// Φ u+03A6 D6
// φ u+03C6 F6
// Χ u+03A7 D7
// χ u+03C7 F7
// Ψ u+03A8 D8
// ψ u+03C8 F8
// Ω u+03A9 D9
// ω u+03C9 F9
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
19 changes: 19 additions & 0 deletions snippets/csharp/System.Text/StringBuilder/Overview/call1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// <Snippet4>
using System;
using System.Text;

public class Example
{
public static void Main()
{
StringBuilder sb = new StringBuilder();
sb.Append("This is the beginning of a sentence, ");
sb.Replace("the beginning of ", "");
sb.Insert(sb.ToString().IndexOf("a ") + 2, "complete ");
sb.Replace(",", ".");
Console.WriteLine(sb.ToString());
}
}
// The example displays the following output:
// This is a complete sentence.
// </Snippet4>
17 changes: 17 additions & 0 deletions snippets/csharp/System.Text/StringBuilder/Overview/call2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <Snippet5>
using System;
using System.Text;

public class Example2
{
public static void Main()
{
StringBuilder sb = new StringBuilder("This is the beginning of a sentence, ");
sb.Replace("the beginning of ", "").Insert(sb.ToString().IndexOf("a ") + 2,
"complete ").Replace(",", ".");
Console.WriteLine(sb.ToString());
}
}
// The example displays the following output:
// This is a complete sentence.
// </Snippet5>
46 changes: 46 additions & 0 deletions snippets/csharp/System.Text/StringBuilder/Overview/chars1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <Snippet7>
using System;
using System.Globalization;
using System.Text;

public class Example3
{
public static void Main()
{
Random rnd = new Random();
StringBuilder sb = new StringBuilder();

// Generate 10 random numbers and store them in a StringBuilder.
for (int ctr = 0; ctr <= 9; ctr++)
sb.Append(rnd.Next().ToString("N5"));

Console.WriteLine("The original string:");
Console.WriteLine(sb.ToString());

// Decrease each number by one.
for (int ctr = 0; ctr < sb.Length; ctr++)
{
if (Char.GetUnicodeCategory(sb[ctr]) == UnicodeCategory.DecimalDigitNumber)
{
int number = (int)Char.GetNumericValue(sb[ctr]);
number--;
if (number < 0) number = 9;

sb[ctr] = number.ToString()[0];
}
}
Console.WriteLine("\nThe new string:");
Console.WriteLine(sb.ToString());
}
}
// The example displays the following output:
// The original string:
// 1,457,531,530.00000940,522,609.000001,668,113,564.000001,998,992,883.000001,792,660,834.00
// 000101,203,251.000002,051,183,075.000002,066,000,067.000001,643,701,043.000001,702,382,508
// .00000
//
// The new string:
// 0,346,420,429.99999839,411,598.999990,557,002,453.999990,887,881,772.999990,681,559,723.99
// 999090,192,140.999991,940,072,964.999991,955,999,956.999990,532,690,932.999990,691,271,497
// .99999
// </Snippet7>
45 changes: 45 additions & 0 deletions snippets/csharp/System.Text/StringBuilder/Overview/default1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <Snippet3>
using System;
using System.Reflection;
using System.Text;

public class Example4
{
public static void Main()
{
StringBuilder sb = new StringBuilder();
ShowSBInfo(sb);
sb.Append("This is a sentence.");
ShowSBInfo(sb);
for (int ctr = 0; ctr <= 10; ctr++)
{
sb.Append("This is an additional sentence.");
ShowSBInfo(sb);
}
}

private static void ShowSBInfo(StringBuilder sb)
{
foreach (var prop in sb.GetType().GetProperties())
{
if (prop.GetIndexParameters().Length == 0)
Console.Write("{0}: {1:N0} ", prop.Name, prop.GetValue(sb));
}
Console.WriteLine();
}
}
// The example displays the following output:
// Capacity: 16 MaxCapacity: 2,147,483,647 Length: 0
// Capacity: 32 MaxCapacity: 2,147,483,647 Length: 19
// Capacity: 64 MaxCapacity: 2,147,483,647 Length: 50
// Capacity: 128 MaxCapacity: 2,147,483,647 Length: 81
// Capacity: 128 MaxCapacity: 2,147,483,647 Length: 112
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 143
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 174
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 205
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 236
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 267
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 298
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 329
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 360
// </Snippet3>
44 changes: 44 additions & 0 deletions snippets/csharp/System.Text/StringBuilder/Overview/delete1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <Snippet10>
using System;
using System.Text;

public class Example5
{
public static void Main()
{
StringBuilder sb = new StringBuilder("A StringBuilder object");
ShowSBInfo(sb);
// Remove "object" from the text.
string textToRemove = "object";
int pos = sb.ToString().IndexOf(textToRemove);
if (pos >= 0)
{
sb.Remove(pos, textToRemove.Length);
ShowSBInfo(sb);
}
// Clear the StringBuilder contents.
sb.Clear();
ShowSBInfo(sb);
}

public static void ShowSBInfo(StringBuilder sb)
{
Console.WriteLine($"\nValue: {sb.ToString()}");
foreach (var prop in sb.GetType().GetProperties())
{
if (prop.GetIndexParameters().Length == 0)
Console.Write("{0}: {1:N0} ", prop.Name, prop.GetValue(sb));
}
Console.WriteLine();
}
}
// The example displays the following output:
// Value: A StringBuilder object
// Capacity: 22 MaxCapacity: 2,147,483,647 Length: 22
//
// Value: A StringBuilder
// Capacity: 22 MaxCapacity: 2,147,483,647 Length: 16
//
// Value:
// Capacity: 22 MaxCapacity: 2,147,483,647 Length: 0
// </Snippet10>
48 changes: 48 additions & 0 deletions snippets/csharp/System.Text/StringBuilder/Overview/expand1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// <Snippet9>
using System;
using System.Text;

public class Example6
{
public static void Main()
{
// Create a StringBuilder object with no text.
StringBuilder sb = new StringBuilder();
// Append some text.
sb.Append('*', 10).Append(" Adding Text to a StringBuilder Object ").Append('*', 10);
sb.AppendLine("\n");
sb.AppendLine("Some code points and their corresponding characters:");
// Append some formatted text.
for (int ctr = 50; ctr <= 60; ctr++)
{
sb.AppendFormat("{0,12:X4} {1,12}", ctr, Convert.ToChar(ctr));
sb.AppendLine();
}
// Find the end of the introduction to the column.
int pos = sb.ToString().IndexOf("characters:") + 11 +
Environment.NewLine.Length;
// Insert a column header.
sb.Insert(pos, $"{Environment.NewLine}{"Code Unit",12} {"Character",12}{Environment.NewLine}");

// Convert the StringBuilder to a string and display it.
Console.WriteLine(sb.ToString());
}
}
// The example displays the following output:
// ********** Adding Text to a StringBuilder Object **********
//
// Some code points and their corresponding characters:
//
// Code Unit Character
// 0032 2
// 0033 3
// 0034 4
// 0035 5
// 0036 6
// 0037 7
// 0038 8
// 0039 9
// 003A :
// 003B ;
// 003C <
// </Snippet9>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <Snippet1>
using System;

public class Example7
{
public unsafe static void Main()
{
string value = "This is the first sentence" + ".";
fixed (char* start = value)
{
value = String.Concat(value, "This is the second sentence. ");
fixed (char* current = value)
{
Console.WriteLine(start == current);
}
}
}
}
// The example displays the following output:
// False
// </Snippet1>
Loading
Loading