String.Length Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan jumlah karakter dalam objek saat ini String .
public:
property int Length { int get(); };
public int Length { get; }
member this.Length : int
Public ReadOnly Property Length As Integer
Nilai Properti
Jumlah karakter dalam string saat ini.
Contoh
Contoh berikut menunjukkan Length properti .
string str = "abcdefg";
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length);
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length);
int length = str.Length;
Console.WriteLine("3) The length of '{0}' is {1}", str, length);
// This example displays the following output:
// 1) The length of 'abcdefg' is 7
// 2) The length of 'xyz' is 3
// 3) The length of 'abcdefg' is 7
let str = "abcdefg"
printfn $"1) The length of '{str}' is {str.Length}"
printfn $"""2) The length of '{"xyz"}' is {"xyz".Length}"""
let length = str.Length
printfn $"3) The length of '{str}' is {length}"
// This example displays the following output:
// 1) The length of 'abcdefg' is 7
// 2) The length of 'xyz' is 3
// 3) The length of 'abcdefg' is 7
Class Sample
Public Shared Sub Main()
Dim str As String = "abcdefg"
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length)
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length)
Dim length As Integer = str.Length
Console.WriteLine("1) The length of '{0}' is {1}", str, length)
End Sub
End Class
'
'This example displays the following output:
' 1) The length of 'abcdefg' is 7
' 2) The length of 'xyz' is 3
' 3) The length of 'abcdefg' is 7
Keterangan
Properti Length mengembalikan jumlah Char objek dalam instans ini, bukan jumlah karakter Unicode. Alasannya adalah bahwa karakter Unicode mungkin diwakili oleh lebih dari satu Char. System.Globalization.StringInfo Gunakan kelas untuk bekerja dengan setiap karakter Unicode alih-alih masing-masing Char.
Dalam beberapa bahasa, seperti C dan C++, karakter null menunjukkan akhir string. Di .NET, karakter null dapat disematkan dalam string. Saat string menyertakan satu atau beberapa karakter null, string tersebut disertakan dalam panjang total string. Misalnya, dalam string berikut, substring "abc" dan "def" dipisahkan oleh karakter null. Properti Length mengembalikan 7, yang menunjukkan bahwa properti menyertakan enam karakter alfabet serta karakter null.
string characters = "abc\u0000def";
Console.WriteLine(characters.Length); // Displays 7
let characters = "abc\u0000def"
printfn $"{characters.Length}" // Displays 7
Imports System.Text
Module Example
Public Sub Main()
Dim characters As String = "abc" + ChrW(0) + "def"
Console.WriteLine(characters.Length) ' Displays 7
End Sub
End Module