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
        }
    }
}
相关推荐
d111111111d13 分钟前
STM32中为什么会有APB1和APB2两个外设有什么区别
笔记·stm32·单片机·嵌入式硬件·学习
wnety17 分钟前
C#开发winform调用软键盘
c#·winform
谢大旭44 分钟前
iframe 三档预览模式
c#
化作星辰1 小时前
深度学习_神经网络中最常用的学习率优化算法
深度学习·神经网络·学习
她说彩礼65万1 小时前
C# Sqlite帮助类
jvm·sqlite·c#
71-31 小时前
C语言练习题——判断水仙花数(0-100000)
c语言·笔记·学习
FAREWELL000751 小时前
Lua学习记录(3) --- Lua中的复杂数据类型_table
开发语言·学习·lua
q***18061 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
Broken Arrows2 小时前
排查网络问题的一些工具的作用和常用使用方法
linux·网络·学习
摇滚侠2 小时前
GIT版本管理工具轻松入门 | TortoiseGit,Git 介绍软件安装配置,笔记01
笔记·git