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 thisint 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
        }
    }
}
相关推荐
Mico183 分钟前
MySQL 8.0.35 源码编译安装笔记
数据库·笔记·mysql
秋雨梧桐叶落莳8 分钟前
iOS——3GShare项目总结
学习·macos·ios·objective-c·cocoa
薛定e的猫咪1 小时前
在 vibe coding开发项目过程中使用过的命令和概念整理
人工智能·学习·算法·开源
灵性(๑>ڡ<)☆1 小时前
Java学习笔记--面向对象高级(接口)
笔记·学习
她说彩礼65万1 小时前
C# yeild的使用
开发语言·c#
吴可可1231 小时前
C# CAD二次开发找最近点
c#
bush41 小时前
嵌入式linux学习记录十八,KGDB使用例
linux·学习
fogota1 小时前
【AI】C# .NET8 WPF 第三方DLL引用配置(编译不丢失、不自动删除)
c#·.net·wpf·dll
诗句藏于尽头1 小时前
unlock-music项目安装依赖及启动相关报错问题及解决
学习
91刘仁德1 小时前
MySQL 数据类型详解
android·笔记·mysql·adb