C# String Remove(Int32, Int32)

Description

String Remove(Int32, Int32) returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

Syntax

String.Remove(Int32, Int32) has the following syntax.


public string Remove(
  int startIndex,
  int count
)

Parameters

String.Remove(Int32, Int32) has the following parameters.

  • startIndex - The zero-based position to begin deleting characters.
  • count - The number of characters to delete.

Returns

String.Remove(Int32, Int32) method returns A new string that is equivalent to this instance except for the removed characters.

Example

The following example demonstrates how you can remove the middle name from a complete name.


using System;/*ww  w  .j a v  a 2s.  c o  m*/

public class RemoveTest {
    public static void Main() {

        string name = "this is a test";

        int foundS1 = name.IndexOf(" ");
        int foundS2 = name.IndexOf(" ", foundS1 + 1);

        if (foundS1 != foundS2 && foundS1 >= 0) {
            name = name.Remove(foundS1 + 1, foundS2 - foundS1);
            Console.WriteLine(name);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version