C# String Remove()

字符串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')
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战