c# 学习笔记 - String

文章目录

    • [1. 介绍](#1. 介绍)
      • [1.1 基本介绍](#1.1 基本介绍)
      • [1.2 深层细节](#1.2 深层细节)
    • [2. 构造和属性](#2. 构造和属性)
      • [2.1 String 构造](#2.1 String 构造)
      • [2.2 String 属性](#2.2 String 属性)
    • [3. 方法](#3. 方法)
      • [3.1 字符串判定](#3.1 字符串判定)

1. 介绍

1.1 基本介绍

**  类结构**

**  String官方参考文档:String类**

1.2 深层细节

2. 构造和属性

2.1 String 构造

**  相关构造方法**

csharp 复制代码
class Test {
    static void Main(string[] args) {
        string s = new string("hello, world.");
    }
}

2.2 String 属性

  1. public char this[int index] { get; } 获取指定位置的字符
  2. public int Length { get; } 获取当前String对象中字符数
csharp 复制代码
class Test {
    static void Main() {
        string str = "hello, world";

        Console.WriteLine(str.Length);
        Console.WriteLine(str[1]);
    }
}
/*
12
e
*/

3. 方法

3.1 字符串判定

**  总结**

  1. public static bool IsNullOrEmpty (string? value); --> 字符串为null 或者为空
  2. public static bool IsNullOrWhiteSpace (string? value); --> 字符串为 NULL 或者为 空 或者仅由空白字符组成

**  代码**

csharp 复制代码
class Test {
    static void Main() {
        string[] s = new string[] {
            null, 
            String.Empty, 
            "", 
            "  \t  ", 
            "hello" 
        };


        for(int i = 0; i < s.Length; i ++) {
            Console.Write(String.IsNullOrEmpty(s[i]) + " "); // True True True False False
        }
        Console.WriteLine();

        for(int i = 0; i < s.Length; i++) { 
            Console.Write(String.IsNullOrWhiteSpace(s[i]) + " "); // True True True True False
        }
    }
}
相关推荐
数字芯片实验室5 小时前
分享一个可以学习正则表达式的网址:Pythex.org
学习·正则表达式
陈洪奇5 小时前
注册中心学习笔记整理
笔记·学习
光影少年5 小时前
从前端转go开发的学习路线
前端·学习·golang
我是苏苏7 小时前
C#基础:Winform桌面开发中窗体之间的数据传递
开发语言·c#
兴趣使然_8 小时前
【笔记】使用 html 创建网址快捷方式
笔记·html·js
aramae10 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
fen_fen10 小时前
学习笔记(32):matplotlib绘制简单图表-数据分布图
笔记·学习·matplotlib
水果里面有苹果14 小时前
20-C#构造函数--虚方法
java·前端·c#
饕餮争锋14 小时前
设计模式笔记_创建型_建造者模式
笔记·设计模式·建造者模式
萝卜青今天也要开心14 小时前
2025年上半年软件设计师考后分享
笔记·学习