String.Remove Metode

Definisi

Mengembalikan string baru di mana jumlah karakter tertentu dari string saat ini dihapus.

Overload

Remove(Int32)

Mengembalikan string baru di mana semua karakter dalam instans saat ini, dimulai pada posisi tertentu dan melanjutkan melalui posisi terakhir, telah dihapus.

Remove(Int32, Int32)

Mengembalikan string baru di mana sejumlah karakter tertentu dalam instans saat ini yang dimulai pada posisi tertentu telah dihapus.

Remove(Int32)

Sumber:
String.Manipulation.cs
Sumber:
String.Manipulation.cs
Sumber:
String.Manipulation.cs

Mengembalikan string baru di mana semua karakter dalam instans saat ini, dimulai pada posisi tertentu dan melanjutkan melalui posisi terakhir, telah dihapus.

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

Parameter

startIndex
Int32

Posisi berbasis nol untuk mulai menghapus karakter.

Mengembalikan

String baru yang setara dengan string ini kecuali untuk karakter yang dihapus.

Pengecualian

startIndex kurang dari nol.

-atau-

startIndex menentukan posisi yang tidak ada dalam string ini.

Contoh

Contoh berikut menunjukkan Remove metode . Kasus berikutnya hingga terakhir menghapus semua teks mulai dari indeks yang ditentukan hingga akhir string. Kasus terakhir menghapus tiga karakter mulai dari indeks yang ditentukan.

// 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
'

Keterangan

Dalam .NET Framework, string berbasis nol. Nilai startIndex parameter dapat berkisar dari nol hingga satu kurang dari panjang instans string.

Catatan

Metode ini tidak mengubah nilai instans saat ini. Sebaliknya, ia mengembalikan string baru di mana semua karakter dari posisi startIndex ke akhir string asli telah dihapus.

Lihat juga

Berlaku untuk

Remove(Int32, Int32)

Sumber:
String.Manipulation.cs
Sumber:
String.Manipulation.cs
Sumber:
String.Manipulation.cs

Mengembalikan string baru di mana sejumlah karakter tertentu dalam instans saat ini yang dimulai pada posisi tertentu telah dihapus.

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

Parameter

startIndex
Int32

Posisi berbasis nol untuk mulai menghapus karakter.

count
Int32

Jumlah karakter yang akan dihapus.

Mengembalikan

String baru yang setara dengan instans ini kecuali untuk karakter yang dihapus.

Pengecualian

Baik startIndex atau count kurang dari nol.

-atau-

startIndex plus count tentukan posisi di luar instans ini.

Contoh

Contoh berikut menunjukkan bagaimana Anda dapat menghapus nama tengah dari nama lengkap.

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'

Keterangan

Dalam .NET Framework, string berbasis nol. Nilai startIndex parameter dapat berkisar dari nol hingga satu kurang dari panjang instans string.

Catatan

Metode ini tidak mengubah nilai instans saat ini. Sebaliknya, ia mengembalikan string baru di mana jumlah karakter yang ditentukan oleh count parameter telah dihapus. Karakter dihapus pada posisi yang ditentukan oleh startIndex.

Lihat juga

Berlaku untuk