共用方式為


String.ToString 方法

定義

將這個執行個體的值轉換為 String

多載

ToString()

傳回這個 String 執行個體;不會實際執行轉換。

ToString(IFormatProvider)

傳回這個 String 執行個體;不會實際執行轉換。

ToString()

來源:
String.cs
來源:
String.cs
來源:
String.cs

傳回這個 String 執行個體;不會實際執行轉換。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

目前的字串。

範例

下列範例示範 ToString 方法。請注意,此範例不會明確呼叫 ToString 方法。 相反地,複合 格式 功能會隱含呼叫 方法。

using namespace System;
int main()
{
   String^ str1 = "123";
   String^ str2 = "abc";
   Console::WriteLine( "Original str1: {0}", str1 );
   Console::WriteLine( "Original str2: {0}", str2 );
   Console::WriteLine( "str1 same as str2?: {0}", Object::ReferenceEquals( str1, str2 ) );
   str2 = str1;
   Console::WriteLine();
   Console::WriteLine( "New str2:      {0}", str2 );
   Console::WriteLine( "str1 same as str2?: {0}", Object::ReferenceEquals( str1, str2 ) );
}

/*
This code produces the following output:
Original str1: 123
Original str2: abc
str1 same as str2?: False

New str2:      123
str1 same as str2?: True
*/
using System;

class stringToString {
    public static void Main() {
    String str1 = "123";
    String str2 = "abc";

    Console.WriteLine("Original str1: {0}", str1);
    Console.WriteLine("Original str2: {0}", str2);
    Console.WriteLine("str1 same as str2?: {0}", Object.ReferenceEquals(str1, str2));

    str2 = str1.ToString();
    Console.WriteLine();
    Console.WriteLine("New str2:      {0}", str2);
    Console.WriteLine("str1 same as str2?: {0}", Object.ReferenceEquals(str1, str2));
    }
}
/*
This code produces the following output:
Original str1: 123
Original str2: abc
str1 same as str2?: False

New str2:      123
str1 same as str2?: True
*/
open System

[<EntryPoint>]
let main _ =
    let str1 = "123"
    let str2 = "abc"
    printfn $"Original str1: {str1}"
    printfn $"Original str2: {str2}"
    printfn $"str1 same as str2?: {Object.ReferenceEquals(str1, str2)}"

    let str2 = str1.ToString()
    printfn $"\nNew str2:      {str2}"
    printfn $"str1 same as str2?: {Object.ReferenceEquals(str1, str2)}"
    0

(*
This code produces the following output:
Original str1: 123
Original str2: abc
str1 same as str2?: False

New str2:      123
str1 same as str2?: True
*)
 _

Class stringToString
   
   Public Shared Sub Main()
      Dim str1 As [String] = "123"
      Dim str2 As [String] = "abc"
      
      Console.WriteLine("Original str1: {0}", str1)
      Console.WriteLine("Original str2: {0}", str2)
      Console.WriteLine("str1 same as str2?: {0}", [Object].ReferenceEquals(str1, str2))
      
      str2 = str1.ToString()
      Console.WriteLine()
      Console.WriteLine("New str2:      {0}", str2)
      Console.WriteLine("str1 same as str2?: {0}", [Object].ReferenceEquals(str1, str2))
   End Sub
End Class
'
'This code produces the following output:
'Original str1: 123
'Original str2: abc
'str1 same as str2?: False
'
'New str2:      123
'str1 same as str2?: True
'

備註

因為這個方法只會傳回目前的字串,所以不需要直接呼叫它。 它通常以隱含方式在複合格式設定作業中呼叫,如範例所示。

另請參閱

適用於

ToString(IFormatProvider)

來源:
String.cs
來源:
String.cs
來源:
String.cs

傳回這個 String 執行個體;不會實際執行轉換。

public:
 virtual System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider? provider);
public string ToString (IFormatProvider provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String

參數

provider
IFormatProvider

(保留) 物件,提供特定文化特性格式資訊。

傳回

目前的字串。

實作

備註

provider 已保留,且目前未參與這項作業。

因為這個方法只會傳回目前的字串,所以不需要直接呼叫它。

適用於