C#数据类型:string简单使用

string cmdstr = "Helloworld!";

cmdstr = cmdstr.Remove(cmdstr.Length - 1);//删除最后一个字符

//字符串连接

string str1 = "Hello";

string str2 = "World";

string result1 = str1 + " " + str2;

string result2 = $"{str1} {str2}";//建议使用干方式连接字符

//字符串长度

string str = "Hello";

int length = str.Length;

//字符串插入

string str = "HelloWorld";

string result = str.Insert(5, ", ");

Console.WriteLine(result); // 输出: Hello, World

//字符串删除

string str = "Hello, World!";

string result = str.Remove(5, 2);

Console.WriteLine(result); // 输出: Hello World!

//字符串复制

string str = "Hello";

string copy = string.Copy(str);

Console.WriteLine(copy); // 输出: Hello

//字符串格式化日期

DateTime date = DateTime.Now;

string formattedDate = date.ToString("yyyy-MM-dd HH:mm:ss");

Console.WriteLine(formattedDate); // 输出: 当前日期和时间的格式化字符串

//字符串比较

string str1 = "Hello";

string str2 = "hello";

bool isEqual = string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);

//字符串查找

string str = "Hello, World!";

int index = str.IndexOf("World");

//字符串替换

string str = "Hello, World!";

string result = str.Replace("World", "C#");

//字符串分割

string str = "Hello,World,C#";

string[] parts = str.Split(',');

foreach (var part in parts)

{

Console.WriteLine(part); // 输出: Hello, World, C#

}

//字符串截取

string str = "Hello, World!";

string result = str.Substring(7, 5);

Console.WriteLine(result); // 输出: World

//字符串转小写

string str = "HELLO, WORLD!";

string lowerCase = str.ToLower();

Console.WriteLine(lowerCase); // 输出: hello, world!

//字符串转大写

string str = "hello, world!";

string upperCase = str.ToUpper();

Console.WriteLine(upperCase); // 输出: HELLO, WORLD!

//字符串修剪

string str = " Hello, World! ";

string trimmed = str.Trim();

Console.WriteLine(trimmed); // 输出: Hello, World!

//字符串格式化

string name = "Alice";

int age = 30;

string result = string.Format("Name: {0}, Age: {1}", name, age);

Console.WriteLine(result); // 输出: Name: Alice, Age: 30

//字符串拼接

List<string> words = new List<string> { "Hello", "World", "C#" };

string result = string.Join(", ", words);

Console.WriteLine(result); // 输出: Hello, World, C#

//字符串是否为空或空字符串

string str1 = "";

string str2 = null;

bool isNullOrEmpty1 = string.IsNullOrEmpty(str1);

bool isNullOrEmpty2 = string.IsNullOrEmpty(str2);

Console.WriteLine(isNullOrEmpty1); // 输出: True

Console.IsNullOrWhiteSpace(isNullOrEmpty2); // 输出: True

//字符串编码转换

string str = "Hello, 世界!";

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);

string result = System.Text.Encoding.UTF8.GetString(bytes);

Console.WriteLine(result); // 输出: Hello, 世界!

//字符串反转

string str = "Hello";

char[] charArray = str.ToCharArray();

Array.Reverse(charArray);

string result = new string(charArray);

Console.WriteLine(result); // 输出: olleH

相关推荐
Fɪʀᴇᴡᴏʀᴋs7 分钟前
Oceanbase数据库—基于社区版本的数据库集群管理
运维·数据库·oceanbase
HalvmånEver13 分钟前
Linux:基础开发工具(五)
linux·运维·服务器
qq_2789841315 分钟前
ubuntu vlan网络设置
linux·服务器·网络
Code Warrior20 分钟前
【MySQL数据库】数据类型
android·数据库·mysql
范纹杉想快点毕业27 分钟前
《嵌入式硬件从入门到精通:电源 / 模电 / 数电 / 通信核心全解析》
java·开发语言·数据库·单片机·嵌入式硬件
小二·28 分钟前
Linux 高频面试题(50道 + 详细答案)
linux·运维·服务器
虹科测试测量35 分钟前
德思特干货 | 单通道、多通道衰减器与衰减矩阵:如何选择合适的衰减方案
服务器·测试工具·算法·矩阵
今晚务必早点睡1 小时前
云服务器公共镜像怎么选?一文带你看懂 Ubuntu、Debian、CentOS、Rocky 的最佳使用场景
服务器·ubuntu·debian
FLPGYH1 小时前
BMC 深度解析:服务器带外管理的核心技术架构与实践
linux·服务器·c++·驱动开发
CryptoRzz2 小时前
印度股票数据 PHP 对接文档 覆盖 BSE(孟买证券交易所)和 NSE(印度国家证券交易所)的实时数据
android·服务器·开发语言·区块链·php