字符串Remove()
方法从字符串中移除指定数量的字符。
示例
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str = "Ice cream";
// removes characters from index 2
string result = str.Remove(2);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
// Output: Ic
Remove() 语法
字符串Remove()
方法的语法是
Remove(int startIndex, int count)
这里,Remove()
是 String
类的其中一个方法。
Remove() 参数
Remove()
方法接受以下参数
- startIndex - 开始删除字符的索引
- count (可选) - 要删除的字符数
Remove() 返回值
Remove()
方法返回
- 移除字符后的字符串
示例 1: C# String Remove()
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str = "Chocolate";
// removes characters from index 5
string result = str.Remove(5);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
Choco
这里,
str.Remove(5)
- 从索引 5 开始移除所有字符
示例 2: C# String Remove() 附带 count 参数
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str = "Chocolate";
// removes 2 chars from index 5
string result = str.Remove(5, 2);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
Chocote
这里,
str.Remove(5, 2)
- 从索引 5 开始移除 2 个字符 ('l'
和'a'
)