From 5c7df47f6a78c61fc0c4778bcac3fc74715de1b6 Mon Sep 17 00:00:00 2001 From: Ron Petrusha Date: Tue, 4 Jun 2019 14:16:40 -0700 Subject: [PATCH] Examples for String.Copy alternatives --- .../csharp/api/system/string/copy/Program.cs | 82 +++++++++++++++++++ .../csharp/api/system/string/copy/copy.csproj | 10 +++ .../api/system/string/copy/Program.vb | 39 +++++++++ .../api/system/string/copy/copy.vbproj | 9 ++ 4 files changed, 140 insertions(+) create mode 100644 snippets/csharp/api/system/string/copy/Program.cs create mode 100644 snippets/csharp/api/system/string/copy/copy.csproj create mode 100644 snippets/visualbasic/api/system/string/copy/Program.vb create mode 100644 snippets/visualbasic/api/system/string/copy/copy.vbproj diff --git a/snippets/csharp/api/system/string/copy/Program.cs b/snippets/csharp/api/system/string/copy/Program.cs new file mode 100644 index 00000000000..791b699f38e --- /dev/null +++ b/snippets/csharp/api/system/string/copy/Program.cs @@ -0,0 +1,82 @@ +using System; +using System.Runtime.InteropServices; + +namespace copy +{ + class Program + { + static void Main() + { + PerformStringOperation(); + Console.WriteLine("---"); + UseMutableBuffer(); + Console.WriteLine("---"); + UseUnmanaged(); + } + + private static void PerformStringOperation() + { + // + var original = "This is a sentence. This is a second sentence."; + var sentence1 = original.Substring(0, original.IndexOf(".") + 1); + Console.WriteLine(original); + Console.WriteLine(sentence1); + // The example displays the following output: + // This is a sentence. This is a second sentence. + // This is a sentence. + // + } + + // + private static void UseMutableBuffer() + { + var original = "This is a sentence. This is a second sentence."; + var chars = original.ToCharArray(); + var span = new Span(chars); + var slice = span.Slice(span.IndexOf('.'), 3); + slice = MergeSentence(slice); + Console.WriteLine($"Original string: {original}"); + Console.WriteLine($"Modified string: {span.ToString()}"); + + static Span MergeSentence(Span span) + { + if (span.Length == 0) return Span.Empty; + + span[0] = ';'; + span[2] = Char.ToLower(span[2]); + return span; + } + } + // The example displays the following output: + // Original string: This is a sentence. This is a second sentence. + // Modified string: This is a sentence; this is a second sentence. + // + + // + private static void UseUnmanaged() + { + var original = "This is a single sentence."; + var len = original.Length; + var ptr = Marshal.StringToHGlobalUni(original); + string result; + unsafe + { + char *ch = (char *) ptr.ToPointer(); + while (len-- > 0) + { + char c = Convert.ToChar(Convert.ToUInt16(*ch) + 1); + *ch++ = c; + } + result = Marshal.PtrToStringUni(ptr); + Marshal.FreeHGlobal(ptr); + } + Console.WriteLine($"Original string: {original}"); + Console.WriteLine($"String from interop: '{result}'"); + } + // The example displays the following output: + // Original string: This is a single sentence. + // String from interop: 'Uijt!jt!b!tjohmf!tfoufodf/' + // + } +} + diff --git a/snippets/csharp/api/system/string/copy/copy.csproj b/snippets/csharp/api/system/string/copy/copy.csproj new file mode 100644 index 00000000000..5d825af2182 --- /dev/null +++ b/snippets/csharp/api/system/string/copy/copy.csproj @@ -0,0 +1,10 @@ + + + + Exe + netcoreapp3.0 + 8.0 + True + + + diff --git a/snippets/visualbasic/api/system/string/copy/Program.vb b/snippets/visualbasic/api/system/string/copy/Program.vb new file mode 100644 index 00000000000..703b78cf42a --- /dev/null +++ b/snippets/visualbasic/api/system/string/copy/Program.vb @@ -0,0 +1,39 @@ +Imports System +Imports System.Runtime.CompilerServices +Imports System.Text + +Module Program + Sub Main() + PerformStringOperation() + Console.WriteLine("---") + UseMutableBuffer() + Console.WriteLine("---") + End Sub + + Private Sub PerformStringOperation() + ' + Dim original = "This is a sentence. This is a second sentence." + Dim sentence1 = original.Substring(0, original.IndexOf(".") + 1) + Console.WriteLine(original) + Console.WriteLine(sentence1) + ' The example displays the following output: + ' This is a sentence. This is a second sentence. + ' This is a sentence. + ' + End Sub + + ' + Private Sub UseMutableBuffer() + Dim original = "This is a sentence. This is a second sentence." + Dim sb = new StringBuilder(original) + Dim index = original.IndexOf(".") + sb(index) = ";" + sb(index + 2) = Char.ToLower(sb(index + 2)) + Console.WriteLine($"Original string: {original}") + Console.WriteLine($"Modified string: {sb.ToString()}") + End Sub + ' The example displays the following output: + ' Original string: This is a sentence. This is a second sentence. + ' Modified string: This is a sentence; this is a second sentence. + ' +End Module diff --git a/snippets/visualbasic/api/system/string/copy/copy.vbproj b/snippets/visualbasic/api/system/string/copy/copy.vbproj new file mode 100644 index 00000000000..12ebc9f5816 --- /dev/null +++ b/snippets/visualbasic/api/system/string/copy/copy.vbproj @@ -0,0 +1,9 @@ + + + + Exe + copy.vb + netcoreapp3.0 + + +