C#的String.Length属性示例
C#String.Length属性
String.Length属性用于获取字符串对象中的字符总数(字符串的长度);它使用此字符串进行调用并返回字符总数。
语法:
public int Length { get; }参数:无。
返回值:int–它返回字符串/字符串长度中的字符总数。
示例
Input:
string str = "nhooo";
function call:
str.Length;
Output:
11C#使用String.Length属性获取字符串长度的示例
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//字符串变量声明
string str = "nhooo";
//打印字串及其长度
Console.WriteLine("str: " + str);
Console.WriteLine("str length: " + str.Length);
//将“”分配给字符串"" to the string
//并打印其长度
str = "";
Console.WriteLine("After assigning \"\" to the str...");
Console.WriteLine("str: " + str);
Console.WriteLine("str length: " + str.Length);
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
str: nhooo str length: 11 After assigning "" to the str... str: str length: 0
参考:String.Length物业