String.Intern(String) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Retrieves the system's reference to the specified String.
public:
static System::String ^ Intern(System::String ^ str);
public static string Intern (string str);
static member Intern : string -> string
Public Shared Function Intern (str As String) As String
- str
- String
A string to search for in the intern pool.
The system's reference to str
, if it is interned; otherwise, a new reference to a string with the value of str
.
str
is null
.
The following example uses three strings that are equal in value to determine whether a newly created string and an interned string are equal.
// Sample for String::Intern(String)
using namespace System;
using namespace System::Text;
int main()
{
String^ s1 = "MyTest";
String^ s2 = (gcnew StringBuilder)->Append( "My" )->Append( "Test" )->ToString();
String^ s3 = String::Intern( s2 );
Console::WriteLine( "s1 == '{0}'", s1 );
Console::WriteLine( "s2 == '{0}'", s2 );
Console::WriteLine( "s3 == '{0}'", s3 );
Console::WriteLine( "Is s2 the same reference as s1?: {0}", s2 == s1 );
Console::WriteLine( "Is s3 the same reference as s1?: {0}", s3 == s1 );
}
/*
This example produces the following results:
s1 == 'MyTest'
s2 == 'MyTest'
s3 == 'MyTest'
Is s2 the same reference as s1?: False
Is s3 the same reference as s1?: True
*/
// Sample for String.Intern(String)
using System;
using System.Text;
class Sample
{
public static void Main()
{
string s1 = "MyTest";
string s2 = new StringBuilder().Append("My").Append("Test").ToString();
string s3 = String.Intern(s2);
Console.WriteLine($"s1 == {s1}");
Console.WriteLine($"s2 == {s2}");
Console.WriteLine($"s3 == {s3}");
Console.WriteLine($"Is s2 the same reference as s1?: {(Object)s2 == (Object)s1}");
Console.WriteLine($"Is s3 the same reference as s1?: {(Object)s3 == (Object)s1}");
}
}
/*
This example produces the following results:
s1 == MyTest
s2 == MyTest
s3 == MyTest
Is s2 the same reference as s1?: False
Is s3 the same reference as s1?: True
*/
// Sample for String.Intern(String)
open System
open System.Text
let s1 = "MyTest"
let s2 = StringBuilder().Append("My").Append("Test").ToString()
let s3 = String.Intern s2
printfn $"s1 = {s1}"
printfn $"s2 = {s2}"
printfn $"s3 = {s3}"
printfn $"Is s2 the same reference as s1?: {s2 :> obj = s1 :> obj}"
printfn $"Is s3 the same reference as s1?: {s3 :> obj = s1 :> obj}"
(*
This example produces the following results:
s1 = MyTest
s2 = MyTest
s3 = MyTest
Is s2 the same reference as s1?: False
Is s3 the same reference as s1?: True
*)
Imports System.Text
Class Sample
Public Shared Sub Main()
Dim s1 As String = "MyTest"
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()
Dim s3 As String = String.Intern(s2)
Console.WriteLine($"s1 = {s1}")
Console.WriteLine($"s2 = {s2}")
Console.WriteLine($"s3 = {s3}")
Console.WriteLine($"Is s2 the same reference as s1?: {s2 Is s1}")
Console.WriteLine($"Is s3 the same reference as s1?: {s3 Is s1}")
End Sub
End Class
'
's1 = MyTest
's2 = MyTest
's3 = MyTest
'Is s2 the same reference as s1?: False
'Is s3 the same reference as s1?: True
'
For more information about this API, see Supplemental API remarks for String.Intern.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: