다음을 통해 공유


String.Intern(String) 메서드

정의

지정된 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

내부 풀에서 검색할 문자열입니다.

반환

str이 내부 풀에 추가되었으면 해당 시스템 참조이고, 그렇지 않으면 str 값을 가진 문자열에 대한 새 참조입니다.

예외

strnull입니다.

예제

다음 예제에서는 값이 같은 세 개의 문자열을 사용하여 새로 만든 문자열과 인턴 문자열이 같은지 여부를 확인합니다.

// 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
'

설명

이 API에 대한 자세한 내용은 String.Intern에 대한 추가 API 설명을 참조하세요.

적용 대상

추가 정보