C# 字符串(String)


前言

C#基础学习入门系列- C# 字符串(String)


一、C#字符串(String)

C#字符串(String)是一种不可变的序列字符。任何对字符串的操作都会返回一个新的字符串。字符串在C#中是一个引用类型,使用System.String类表示。

字符串可以通过使用双引号或者@符号来创建。双引号用于创建普通字符串,例如:

string str1 = "Hello World";
string str2 = "C#";

@符号用于创建原始字符串,不会对转义字符进行处理,例如:

string str3 = @"C:\Windows\System32";

在C#中,字符串可以通过+运算符进行连接 例如:

string str4 = str1 + ", " + str2;

也可以使用插入占位符($)来进行字符串插值,例如:

string str5 = $"{str1}, {str2}";

**也可以使用new关键字创建字符串对象,**例如:

string str = new string('a', 5);  // 创建一个由5个'a'字符组成的字符串

访问字符串中的字符:可以使用索引运算符[]来访问字符串中的单个字符,索引从0开始计数,例如:

char firstChar = str[0];

格式化字符串:可以使用字符串插值(string interpolation)或者string.Format()方法来格式化字符串,例如:

int age = 20;
string name = "John";
string message = $"My name is {name} and I am {age} years old.";
string formattedMessage = string.Format("My name is {0} and I am {1} years old.", name, age);

字符串还有许多有用的方法,例如Substring()、Length、ToUpper()、ToLower()等,可以对字符串进行各种操作和处理。

需要注意的是,字符串是不可变的,也就是说,一旦被创建,就无法修改。每次进行字符串操作时,实际上是创建了一个新的字符串对象。这点需要注意,因为在处理大量字符串时,频繁的字符串操作可能会造成性能问题。如果需要频繁修改字符串,可以使用System.Text.StringBuilder类来代替字符串。

二、String 类的属性/方法

C#的String类有以下几个常用的属性:

  1. Length:获取字符串的长度,即字符的数量。例如:
string s = "Hello"; int len = s.Length; // len的值为5。
  1. IsNullOrEmpty:判断字符串是否为null或者空字符串。例如:
string s = ""; 
bool result = string.IsNullOrEmpty(s); // result的值为true。
  1. IsNullOrWhiteSpace:判断字符串是否为null、空字符串或者由空格组成的字符串。例如:
string s = "    "; 
bool result = string.IsNullOrWhiteSpace(s); // result的值为true。
  1. Chars:通过索引获取字符串中指定位置的字符。例如:
string s = "Hello"; char c = s[0]; // c的值为'H'。
  1. ToUpper:将字符串转换为大写字母。例如:
string s = "hello"; string upper = s.ToUpper(); // upper的值为"HELLO"。
  1. ToLower:将字符串转换为小写字母。例如:
string s = "HELLO"; 
string lower = s.ToLower(); // lower的值为"hello"。
  1. Trim:去除字符串两端的空格。例如:
string s = "  Hello  "; 
string trimmed = s.Trim(); // trimmed的值为"Hello"。
  1. StartsWith:判断字符串是否以指定的字符串开始。例如:
string s = "Hello, world"; 
bool result = s.StartsWith("Hello"); // result的值为true。
  1. EndsWith:判断字符串是否以指定的字符串结束。例如:
 string s = "Hello, world"; 
 bool result = s.EndsWith("world"); // result的值为true。
  1. Contains:判断字符串是否包含指定的子字符串。例如:
string s = "Hello, world"; 
bool result = s.Contains("world"); // result的值为true。

这些属性可以帮助我们对字符串进行常见的操作和判断。


总结

以上就是今天的内容了,C#String的简单介绍,以及常用的属性/方法使用

相关推荐

  1. C# 字符串String

    2024-01-21 20:34:01       37 阅读
  2. string字符串c++)

    2024-01-21 20:34:01       11 阅读
  3. Standard C String & Character(标准c字符字符串)

    2024-01-21 20:34:01       18 阅读
  4. C++ 动态字符串String的介绍及经典用法展示

    2024-01-21 20:34:01       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-21 20:34:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-21 20:34:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-21 20:34:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-21 20:34:01       18 阅读

热门阅读

  1. datasets的一些使用技巧

    2024-01-21 20:34:01       33 阅读
  2. 【智驾未来】低代码开发:加速创新交融的利器

    2024-01-21 20:34:01       30 阅读
  3. C# 程序结构

    2024-01-21 20:34:01       30 阅读
  4. 【工业智能】VSB Power Line Fault Detection-chapter2

    2024-01-21 20:34:01       32 阅读
  5. 生产告警JVM内存使用率超过80%告警问题排查

    2024-01-21 20:34:01       33 阅读
  6. python求解特殊回文数

    2024-01-21 20:34:01       31 阅读
  7. html Canvas粒子文字特效

    2024-01-21 20:34:01       34 阅读
  8. bash 5.2中文修订1

    2024-01-21 20:34:01       33 阅读