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
        }
    }
}
相关推荐
爱莉希雅&&&13 小时前
MySQL MGR 组复制 完整笔记
linux·数据库·笔记·mysql·mgr·数据库同步
Alice-YUE13 小时前
ai对话平台中的functioncalling+mcp
前端·笔记·学习·语言模型
峥无14 小时前
Linux进程控制完全笔记(fork→exec→wait→Shell)
linux·笔记·unix
王的宝库14 小时前
【K8s】集群安全机制(二):授权(Authorization)详解与实战
学习·云原生·容器·kubernetes
ReaF_star14 小时前
K8s Pod调度【学习笔记】
笔记·学习·kubernetes
xiaoshuaishuai814 小时前
C# 实现不掉线的CRM
开发语言·c#
程序员大辉14 小时前
Beaver Notes(海狸笔记)v4.4.0 中文版 ,开源免费、本地存储、零追踪的笔记软件
笔记·开源
叛逆的小小黄14 小时前
maxent建模结果中响应曲线的美化
经验分享·笔记·r语言·maxent
handler0114 小时前
Linux: 基本指令知识点(3)
linux·服务器·c语言·开发语言·c++·笔记
fengci.14 小时前
ctfshow其他(web408-web432)
android·开发语言·前端·学习·php