C#实现字符串大小写转换的代码。
时间: 2023-04-09 10:01:12 浏览: 85
C是一种编程语言,由Dennis Ritchie在20世纪70年代开发。它是一种高级语言,可以用于编写操作系统、应用程序和嵌入式系统等。C语言具有高效、可移植、灵活和可扩展等特点,是计算机科学中最重要的编程语言之一。
相关问题
C#字符串大小写统一识别的代码
你可以使用ToLower()或ToUpper()方法将字符串统一转换为小写或大写。
例如:
```csharp
string str = "Hello World";
string lowerStr = str.ToLower(); // 转换为小写
string upperStr = str.ToUpper(); // 转换为大写
```
如果你想要在不改变原始字符串的情况下修改字符串大小写,你可以使用String.ToLower()或String.ToUpper()方法。
例如:
```csharp
string str = "Hello World";
string modifiedStr = str.ToLower(); // 转换为小写,但是原始字符串不变
Console.WriteLine(str); // "Hello World"
Console.WriteLine(modifiedStr); // "hello world"
```
C#字符串
C# 中的字符串是一种引用类型,表示一组 Unicode 字符。可以使用字符串字面量或字符串构造函数来创建字符串对象。以下是一些常见的字符串操作:
1. 字符串连接:使用加号操作符(+)或字符串插值($"{variable}")。
2. 字符串比较:使用 Equals() 方法或 == 运算符。
3. 字符串查找:使用 IndexOf() 或 LastIndexOf() 方法。
4. 字符串分割:使用 Split() 方法。
5. 字符串替换:使用 Replace() 方法。
6. 字符串大小写转换:使用 ToUpper() 或 ToLower() 方法。
7. 字符串格式化:使用 String.Format() 方法或字符串插值。
以下是一个示例:
```
string str1 = "Hello";
string str2 = "world";
string str3 = str1 + " " + str2; // 字符串连接
Console.WriteLine(str3); // 输出 "Hello world"
bool isEqual = str1.Equals("hello", StringComparison.OrdinalIgnoreCase); // 字符串比较
Console.WriteLine(isEqual); // 输出 True
int index = str3.IndexOf("world"); // 字符串查找
Console.WriteLine(index); // 输出 6
string[] parts = str3.Split(' '); // 字符串分割
Console.WriteLine(parts[0]); // 输出 "Hello"
string replaced = str3.Replace("world", "everyone"); // 字符串替换
Console.WriteLine(replaced); // 输出 "Hello everyone"
string upper = str1.ToUpper(); // 字符串大小写转换
Console.WriteLine(upper); // 输出 "HELLO"
string formatted = String.Format("{0} {1}!", str1, str2); // 字符串格式化
Console.WriteLine(formatted); // 输出 "Hello world!"
```