Skip to content

Commit 1cb65b2

Browse files
author
Ron Petrusha
authored
Examples for String.Copy alternatives (#949)
1 parent 4aea8bb commit 1cb65b2

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace copy
5+
{
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
PerformStringOperation();
11+
Console.WriteLine("---");
12+
UseMutableBuffer();
13+
Console.WriteLine("---");
14+
UseUnmanaged();
15+
}
16+
17+
private static void PerformStringOperation()
18+
{
19+
// <Snippet1>
20+
var original = "This is a sentence. This is a second sentence.";
21+
var sentence1 = original.Substring(0, original.IndexOf(".") + 1);
22+
Console.WriteLine(original);
23+
Console.WriteLine(sentence1);
24+
// The example displays the following output:
25+
// This is a sentence. This is a second sentence.
26+
// This is a sentence.
27+
// </Snippet1>
28+
}
29+
30+
// <Snippet2>
31+
private static void UseMutableBuffer()
32+
{
33+
var original = "This is a sentence. This is a second sentence.";
34+
var chars = original.ToCharArray();
35+
var span = new Span<char>(chars);
36+
var slice = span.Slice(span.IndexOf('.'), 3);
37+
slice = MergeSentence(slice);
38+
Console.WriteLine($"Original string: {original}");
39+
Console.WriteLine($"Modified string: {span.ToString()}");
40+
41+
static Span<char> MergeSentence(Span<char> span)
42+
{
43+
if (span.Length == 0) return Span<char>.Empty;
44+
45+
span[0] = ';';
46+
span[2] = Char.ToLower(span[2]);
47+
return span;
48+
}
49+
}
50+
// The example displays the following output:
51+
// Original string: This is a sentence. This is a second sentence.
52+
// Modified string: This is a sentence; this is a second sentence.
53+
// </Snippet2>
54+
55+
// <Snippet3>
56+
private static void UseUnmanaged()
57+
{
58+
var original = "This is a single sentence.";
59+
var len = original.Length;
60+
var ptr = Marshal.StringToHGlobalUni(original);
61+
string result;
62+
unsafe
63+
{
64+
char *ch = (char *) ptr.ToPointer();
65+
while (len-- > 0)
66+
{
67+
char c = Convert.ToChar(Convert.ToUInt16(*ch) + 1);
68+
*ch++ = c;
69+
}
70+
result = Marshal.PtrToStringUni(ptr);
71+
Marshal.FreeHGlobal(ptr);
72+
}
73+
Console.WriteLine($"Original string: {original}");
74+
Console.WriteLine($"String from interop: '{result}'");
75+
}
76+
// The example displays the following output:
77+
// Original string: This is a single sentence.
78+
// String from interop: 'Uijt!jt!b!tjohmf!tfoufodf/'
79+
// </Snippet3>
80+
}
81+
}
82+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
<LangVersion>8.0</LangVersion>
7+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Imports System
2+
Imports System.Runtime.CompilerServices
3+
Imports System.Text
4+
5+
Module Program
6+
Sub Main()
7+
PerformStringOperation()
8+
Console.WriteLine("---")
9+
UseMutableBuffer()
10+
Console.WriteLine("---")
11+
End Sub
12+
13+
Private Sub PerformStringOperation()
14+
' <Snippet1>
15+
Dim original = "This is a sentence. This is a second sentence."
16+
Dim sentence1 = original.Substring(0, original.IndexOf(".") + 1)
17+
Console.WriteLine(original)
18+
Console.WriteLine(sentence1)
19+
' The example displays the following output:
20+
' This is a sentence. This is a second sentence.
21+
' This is a sentence.
22+
' </Snippet1>
23+
End Sub
24+
25+
' <Snippet2>
26+
Private Sub UseMutableBuffer()
27+
Dim original = "This is a sentence. This is a second sentence."
28+
Dim sb = new StringBuilder(original)
29+
Dim index = original.IndexOf(".")
30+
sb(index) = ";"
31+
sb(index + 2) = Char.ToLower(sb(index + 2))
32+
Console.WriteLine($"Original string: {original}")
33+
Console.WriteLine($"Modified string: {sb.ToString()}")
34+
End Sub
35+
' The example displays the following output:
36+
' Original string: This is a sentence. This is a second sentence.
37+
' Modified string: This is a sentence; this is a second sentence.
38+
' </Snippet2>
39+
End Module
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<RootNamespace>copy.vb</RootNamespace>
6+
<TargetFramework>netcoreapp3.0</TargetFramework>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)