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
        }
    }
}
相关推荐
KoiHeng8 分钟前
操作系统简要知识
linux·笔记
巴伦是只猫1 小时前
【机器学习笔记Ⅰ】11 多项式回归
笔记·机器学习·回归
19H2 小时前
Flink-Source算子状态恢复分析
c#·linq
枯萎穿心攻击4 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
DKPT5 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue6 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
巴伦是只猫6 小时前
【机器学习笔记Ⅰ】13 正则化代价函数
人工智能·笔记·机器学习
好好研究7 小时前
学习栈和队列的插入和删除操作
数据结构·学习
新中地GIS开发老师8 小时前
新发布:26考研院校和专业大纲
学习·考研·arcgis·大学生·遥感·gis开发·地理信息科学
SH11HF9 小时前
小菜狗的云计算之旅,学习了解rsync+sersync实现数据实时同步(详细操作步骤)
学习·云计算