C#字符串操作:拼接、截取、分割等
- 字符串初始化和赋值
string str = "Hello, World!";
这段代码将字符串变量 str
初始化并赋值为 "Hello, World!"。
- 字符串连接
string str1 = "Hello";
string str2 = "World";
string result = str1 + ", " + str2;
Console.WriteLine(result); // 输出:Hello, World!
这段代码将两个字符串连接起来,并用逗号和空格分隔。
- 字符串长度
string str = "Hello, World!";
int length = str.Length;
Console.WriteLine("字符串的长度为:" + length); // 输出:字符串的长度为:13
这段代码获取了字符串的长度,并将其输出。
- 字符串截取
string str = "Hello, World!";
string subStr = str.Substring(7);
Console.WriteLine(subStr); // 输出:World!
这段代码从指定位置开始截取字符串的子串。
- 字符串替换
string str = "Hello, World!";
string replacedStr = str.Replace("World", "John");
Console.WriteLine(replacedStr); // 输出:Hello, John!
这段代码将字符串中的一个子串替换为另一个子串。
- 字符串分割
string str = "Hello, World!";
string[] words = str.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
// 输出:
// Hello,
// World!
这段代码将字符串按照指定的分隔符进行分割,并将结果存储在字符串数组中。
- 字符串转换为大写或小写
string str = "Hello, World!";
string upperCase = str.ToUpper();
string lowerCase = str.ToLower();
Console.WriteLine(upperCase); // 输出:HELLO, WORLD!
Console.WriteLine(lowerCase); // 输出:hello, world!
这段代码将字符串转换为全大写或全小写。
- 字符串是否包含指定的子字符串
string str = "Hello, World!";
bool contains = str.Contains("Hello");
Console.WriteLine(contains); // 输出:True
这段代码检查字符串是否包含指定的子串,并返回布尔值。
- 字符串比较
string str1 = "Hello";
string str2 = "hello";
bool equals = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(equals); // 输出:True
这段代码比较两个字符串是否相等,并忽略大小写。
- 格式化字符串
double value = 123.45;
string formattedStr = string.Format("The value is {0:C2}", value);
Console.WriteLine(formattedStr); // 输出:The value is $123.45
这段代码使用占位符将值插入到字符串中,并进行格式化。
- 去除字符串两端的空格
string str = " Hello, World! ";
string trimmedStr = str.Trim();
Console.WriteLine(trimmedStr); // 输出:Hello, World!
这段代码去除字符串两端的空格。
- 字符串转换为整数
string numStr = "123";
int number;
bool success = int.TryParse(numStr, out number);
if (success)
{
Console.WriteLine(number); // 输出:123
}
这段代码将字符串转换为整数。
- 整数转换为字符串
int num = 456;
string numToStr = num.ToString();
Console.WriteLine(numToStr); // 输出:456
这段代码将整数转换为字符串。
下面是一个包含上述所有用法的完整示例代码:
using System;
class Program
{
static void Main()
{
string str = "Hello, World!";
Console.WriteLine("初始化和赋值: " + str);
string str1 = "Hello";
string str2 = "World";
string result = str1 + ", " + str2;
Console.WriteLine("字符串连接: " + result);
int length = str.Length;
Console.WriteLine("字符串长度: " + length);
string subStr = str.Substring(7);
Console.WriteLine("字符串截取: " + subStr);
string replacedStr = str.Replace("World", "John");
Console.WriteLine("字符串替换: " + replacedStr);
string[] words = str.Split(' ');
Console.WriteLine("字符串分割:");
foreach (string word in words)
{
Console.WriteLine(word);
}
string upperCase = str.ToUpper();
string lowerCase = str.ToLower();
Console.WriteLine("字符串转换为大写: " + upperCase);
Console.WriteLine("字符串转换为小写: " + lowerCase);
bool contains = str.Contains("Hello");
Console.WriteLine("字符串包含子串: " + contains);
string str1 = "Hello";
string str2 = "hello";
bool equals = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine("字符串比较: " + equals);
double value = 123.45;
string formattedStr = string.Format("The value is {0:C2}", value);
Console.WriteLine("格式化字符串: " + formattedStr);
string str = " Hello, World! ";
string trimmedStr = str.Trim();
Console.WriteLine("去除字符串两端的空格: " + trimmedStr);
string numStr = "123";
int number;
bool success = int.TryParse(numStr, out number);
if (success)
{
Console.WriteLine("字符串转换为整数: " + number);
}
int num = 456;
string numToStr = num.ToString();
Console.WriteLine("整数转换为字符串: " + numToStr);
}
}
以上是关于 C# 字符串用法的简要介绍以及每个用法的示例代码和注释。您可以根据需要自行修改和扩展这些代码。如果有任何疑问,请随时提问。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明