String.Remove Metoda

Definice

Vrátí nový řetězec, ve kterém je odstraněn zadaný počet znaků z aktuálního řetězce.

Přetížení

Remove(Int32)

Vrátí nový řetězec, ve kterém byly odstraněny všechny znaky v aktuální instanci začínající na zadané pozici a pokračovat až do poslední pozice.

Remove(Int32, Int32)

Vrátí nový řetězec, ve kterém byl odstraněn zadaný počet znaků v aktuální instanci začínající na zadané pozici.

Remove(Int32)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Vrátí nový řetězec, ve kterém byly odstraněny všechny znaky v aktuální instanci začínající na zadané pozici a pokračovat až do poslední pozice.

public:
 System::String ^ Remove(int startIndex);
public string Remove (int startIndex);
member this.Remove : int -> string
Public Function Remove (startIndex As Integer) As String

Parametry

startIndex
Int32

Pozice od nuly pro zahájení odstraňování znaků.

Návraty

Nový řetězec, který je ekvivalentní tomuto řetězci s výjimkou odebraných znaků.

Výjimky

Hodnota startIndex je menší než nula.

-nebo-

startIndex určuje pozici, která není v tomto řetězci.

Příklady

Následující příklad ukazuje metodu Remove . Předposlouchaný případ odebere veškerý text od zadaného indexu až po konec řetězce. Poslední případ odebere ze zadaného indexu tři znaky.

// This example demonstrates the String.Remove() method.
using namespace System;
int main()
{
   String^ s = "abc---def";
   
   //
   Console::WriteLine( "Index: 012345678" );
   Console::WriteLine( "1)     {0}", s );
   Console::WriteLine( "2)     {0}", s->Remove( 3 ) );
   Console::WriteLine( "3)     {0}", s->Remove( 3, 3 ) );
}

/*
This example produces the following results:

Index: 012345678
1)     abc---def
2)     abc
3)     abcdef

*/
// This example demonstrates the String.Remove() method.
using System;

class Sample
{
    public static void Main()
    {
        string s = "abc---def";

        Console.WriteLine("Index: 012345678");
        Console.WriteLine("1)     {0}", s);
        Console.WriteLine("2)     {0}", s.Remove(3));
        Console.WriteLine("3)     {0}", s.Remove(3, 3));
    }
}
/*
This example produces the following results:

Index: 012345678
1)     abc---def
2)     abc
3)     abcdef

*/
// This example demonstrates the String.Remove() method.
let s = "abc---def"

printfn "Index: 012345678"
printfn $"1)     {s}"
printfn $"2)     {s.Remove 3}"
printfn $"3)     {s.Remove(3, 3)}"
(*
This example produces the following results:

Index: 012345678
1)     abc---def
2)     abc
3)     abcdef

*)
' This example demonstrates the String.Remove() method.
Class Sample
   Public Shared Sub Main()
      Dim s As String = "abc---def"
      '
      Console.WriteLine("Index: 012345678")
      Console.WriteLine("1)     {0}", s)
      Console.WriteLine("2)     {0}", s.Remove(3))
      Console.WriteLine("3)     {0}", s.Remove(3, 3))
   End Sub
End Class
'
'This example produces the following results:
'
'Index: 012345678
'1)     abc---def
'2)     abc
'3)     abcdef
'

Poznámky

V rozhraní .NET Framework jsou řetězce založené na nule. Hodnota parametru může být v rozsahu startIndex od nuly do jedné menší než délka instance řetězce.

Poznámka

Tato metoda neupravuje hodnotu aktuální instance. Místo toho vrátí nový řetězec, ve kterém byly odebrány všechny znaky od pozice startIndex do konce původního řetězce.

Viz také

Platí pro

Remove(Int32, Int32)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

Vrátí nový řetězec, ve kterém byl odstraněn zadaný počet znaků v aktuální instanci začínající na zadané pozici.

public:
 System::String ^ Remove(int startIndex, int count);
public string Remove (int startIndex, int count);
member this.Remove : int * int -> string
Public Function Remove (startIndex As Integer, count As Integer) As String

Parametry

startIndex
Int32

Pozice od nuly pro zahájení odstraňování znaků.

count
Int32

Počet znaků, které se mají odstranit.

Návraty

Nový řetězec, který je ekvivalentní této instanci s výjimkou odebraných znaků.

Výjimky

Buď startIndex je, nebo count je menší než nula.

-nebo-

startIndex plus count zadejte pozici mimo tuto instanci.

Příklady

Následující příklad ukazuje, jak můžete odebrat druhé jméno z úplného názvu.

using namespace System;
int main()
{
   String^ name = "Michelle Violet Banks";
   Console::WriteLine( "The entire name is '{0}'", name );
   
   // remove the middle name, identified by finding the spaces in the middle of the name->->.
   int foundS1 = name->IndexOf( " " );
   int foundS2 = name->IndexOf( " ", foundS1 + 1 );
   if ( foundS1 != foundS2 && foundS1 >= 0 )
   {
      name = name->Remove( foundS1 + 1, foundS2 - foundS1 );
      Console::WriteLine( "After removing the middle name, we are left with '{0}'", name );
   }
}
// The example displays the following output:
//       The entire name is 'Michelle Violet Banks'
//       After removing the middle name, we are left with 'Michelle Banks'
using System;

public class RemoveTest
{
    public static void Main()
    {

        string name = "Michelle Violet Banks";

        Console.WriteLine("The entire name is '{0}'", name);

        // Remove the middle name, identified by finding the spaces in the name.
        int foundS1 = name.IndexOf(" ");
        int foundS2 = name.IndexOf(" ", foundS1 + 1);

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

            Console.WriteLine("After removing the middle name, we are left with '{0}'", name);
        }
    }
}
// The example displays the following output:
//       The entire name is 'Michelle Violet Banks'
//       After removing the middle name, we are left with 'Michelle Banks'
let name = "Michelle Violet Banks"

printfn $"The entire name is '{name}'"

// Remove the middle name, identified by finding the spaces in the name.
let foundS1 = name.IndexOf " "
let foundS2 = name.IndexOf(" ", foundS1 + 1)

if foundS1 <> foundS2 && foundS1 >= 0 then
    let name = name.Remove(foundS1 + 1, foundS2 - foundS1)

    printfn $"After removing the middle name, we are left with '{name}'"
// The example displays the following output:
//       The entire name is 'Michelle Violet Banks'
//       After removing the middle name, we are left with 'Michelle Banks'
Public Class RemoveTest
    
    Public Shared Sub Main()
        Dim name As String = "Michelle Violet Banks"
                
        Console.WriteLine("The entire name is '{0}'", name)
        Dim foundS1 As Integer = name.IndexOf(" ")
        Dim foundS2 As Integer = name.IndexOf(" ", foundS1 + 1)
        If foundS1 <> foundS2 And foundS1 >= 0 Then
            
            ' remove the middle name, identified by finding the spaces in the middle of the name...    
            name = name.Remove(foundS1 + 1, foundS2 - foundS1)
            
            Console.WriteLine("After removing the middle name, we are left with '{0}'", name)
        End If
    End Sub
End Class 
' The example displays the following output:
'       The entire name is 'Michelle Violet Banks'
'       After removing the middle name, we are left with 'Michelle Banks'

Poznámky

V rozhraní .NET Framework jsou řetězce založené na nule. Hodnota parametru může být v rozsahu startIndex od nuly do jedné menší než délka instance řetězce.

Poznámka

Tato metoda neupravuje hodnotu aktuální instance. Místo toho vrátí nový řetězec, ve kterém byl odebrán počet znaků určený parametrem count . Znaky se odeberou na pozici určené parametrem startIndex.

Viz také

Platí pro