首页
/
每日頭條
/
科技
/
c 怎麼求字符串長度
c 怎麼求字符串長度
更新时间:2025-03-17 14:59:45

c 怎麼求字符串長度?代碼 /// <summary> /// 得到一個字符串的長度,中文字節算兩個字符串 /// </summary> /// <param name="source">字符串</param> /// <returns></returns> public static int GetLength(string source) { if (string.IsNullOrEmpty(source)) { return 0; } return System.Text.Encoding.Default.GetBytes(source).Length; },我來為大家講解一下關于c 怎麼求字符串長度?跟着小編一起來看一看吧!

c 怎麼求字符串長度(-字符串函數-得到字符串的長度)1

c 怎麼求字符串長度

代碼

/// <summary> /// 得到一個字符串的長度,中文字節算兩個字符串 /// </summary> /// <param name="source">字符串</param> /// <returns></returns> public static int GetLength(string source) { if (string.IsNullOrEmpty(source)) { return 0; } return System.Text.Encoding.Default.GetBytes(source).Length; }

說明

例:中國abc, 如果直接使用

"中國abc".length,則長度是是5,

如果使用 System.Text.Encoding.Default.GetBytes("中國abc").Length 則長度是7,中文算兩個字節。

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
Copyright 2023-2025 - www.tftnews.com All Rights Reserved