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
        }
    }
}
相关推荐
神仙别闹26 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
Y4090011 小时前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
笑衬人心。1 小时前
TCP 拥塞控制算法 —— 慢启动(Slow Start)笔记
笔记·tcp/ip·php
向宇it1 小时前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
花海如潮淹1 小时前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux
Andy杨2 小时前
20250718-5-Kubernetes 调度-Pod对象:重启策略+健康检查_笔记
笔记·容器·kubernetes
杭州杭州杭州8 小时前
Python笔记
开发语言·笔记·python
斯是 陋室10 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
iFulling11 小时前
【计算机网络】第四章:网络层(上)
学习·计算机网络