Esporta (0) Stampa
Espandi tutto

Metodo String.EndsWith (String)

Aggiornamento: novembre 2007

Consente di determinare se la fine di questa istanza corrisponde alla stringa specificata.

Spazio dei nomi:  System
Assembly:  mscorlib (in mscorlib.dll)

public bool EndsWith(
	string value
)
public boolean EndsWith(
	String value
)
public function EndsWith(
	value : String
) : boolean

Parametri

value
Tipo: System.String
Oggetto String con cui eseguire il confronto.

Valore restituito

Tipo: System.Boolean
true se la fine di questa istanza corrisponde al parametro value; in caso contrario false.

EccezioneCondizione
ArgumentNullException

Il parametro value è null.

Questo metodo confronta il parametro value con la sottostringa alla fine di questa istanza avente la stessa lunghezza del parametro value e quindi specifica se sono uguali. Per essere uguali, il parametro value deve essere un riferimento alla stessa istanza o corrispondere alla fine dell'istanza.

Il metodo consente di eseguire un confronto per parola, con distinzione tra maiuscole e minuscole e in base alle impostazioni cultura, utilizzando le impostazioni cultura correnti.

Nell'esempio di codice riportato di seguito viene illustrato come è possibile utilizzare il metodo EndsWith.

using System;

public class EndsWithTest {
    public static void Main() {

        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line, rather than simply
        // removing the last one.
        // note: HTML markup tags always end in a greater than symbol (>).

        string [] strSource = { "<b>This is bold text</b>", "<H1>This is large Text</H1>",
                "<b><i><font color=green>This has multiple tags</font></i></b>",
                "<b>This has <i>embedded</i> tags.</b>",
                "This line simply ends with a greater than symbol, it should not be modified>" };

        Console.WriteLine("The following lists the items before the ends have been stripped:");
        Console.WriteLine("-----------------------------------------------------------------");

        // print out the initial array of strings
        foreach ( string s in strSource )
            Console.WriteLine( s );

        Console.WriteLine();

        Console.WriteLine("The following lists the items after the ends have been stripped:");
        Console.WriteLine("----------------------------------------------------------------");

        // print out the array of strings
        foreach ( string s in strSource )
            Console.WriteLine( StripEndTags( s ) );
    }

    private static string StripEndTags( string item ) {

            // try to find a tag at the end of the line using EndsWith
            if (item.Trim().EndsWith(">")) {

                // now search for the opening tag...
                int lastLocation = item.LastIndexOf( "</" );

                // remove the identified section, if it is a valid region
                if ( lastLocation >= 0 )
                    item =  item.Substring( 0, lastLocation );
            }

    return item;
    }
}


import System.*;

public class EndsWithTest
{
    public static void main(String[] args)
    {
        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line, 
        // rather than simply removing the last one.
        // note: HTML markup tags always end in a greater than symbol (>).
        String strSource[] =  { "<b>This is bold text</b>", 
            "<H1>This is large Text</H1>", 
            "<b><i><font color=green>This has multiple tags</font></i></b>", 
            "<b>This has <i>embedded</i> tags.</b>", 
            "This line simply ends with a greater than symbol, it should not "
            + "be modified>" };

        Console.WriteLine("The following lists the items before the ends have "
            + "been stripped:");
        Console.WriteLine("---------------------------------------------------"
            + "--------------");

        // print out the initial array of strings
        for (int iCtr = 0; iCtr < strSource.get_Length(); iCtr++) {
            String s = (String)strSource.get_Item(iCtr);
            Console.WriteLine(s);
        }
        Console.WriteLine();
        Console.WriteLine("The following lists the items after the ends have "
            + "been stripped:");
        Console.WriteLine("---------------------------------------------------"
            + "-------------");

        // print out the array of strings
        for (int iCtr = 0; iCtr < strSource.get_Length(); iCtr++) {
            String s = (String)strSource.get_Item(iCtr);
            Console.WriteLine(StripEndTags(s));
        }
    } //main

    private static String StripEndTags(String item)
    {
        // try to find a tag at the end of the line using EndsWith
        if (item.Trim().EndsWith(">")) {
            // now search for the opening tag...
            int lastLocation = item.LastIndexOf("</");
            // remove the identified section, if it is a valid region
            if (lastLocation >= 0) {
                item = item.Substring(0, lastLocation);
            }
        }
        return item;
    } //StripEndTags
} //EndsWithTest


import System;

public class EndsWithTest {
    public static function Main() : void {

        // process an input file that contains html tags.
        // this sample checks for multiple tags at the end of the line, rather than simply
        // removing the last one.
        // note: HTML markup tags always end in a greater than symbol (>).

        var strSource : String [] = [ "<b>This is bold text</b>", "<H1>This is large Text</H1>",
                "<b><i><font color=green>This has multiple tags</font></i></b>",
                "<b>This has <i>embedded</i> tags.</b>",
                "This line simply ends with a greater than symbol, it should not be modified>"];

        Console.WriteLine("The following lists the items before the ends have been stripped:");
        Console.WriteLine("-----------------------------------------------------------------");

        // print out the initial array of strings
        for( var i : int in strSource )
            Console.WriteLine( strSource[i] );

        Console.WriteLine();
        Console.WriteLine("The following lists the items after the ends have been stripped:");
        Console.WriteLine("----------------------------------------------------------------");
        // print out the array of strings
        for( i in strSource )
            Console.WriteLine( StripEndTags( strSource[i] ) );
    }

    private static function StripEndTags( item : String ) : String {
        // try to find a tag at the end of the line using EndsWith
        if (item.Trim().EndsWith(">")) {
            // now search for the opening tag...
            var lastLocation : int = item.LastIndexOf( "</" );
            // remove the identified section, if it is a valid region
            if ( lastLocation >= 0 )
                item =  item.Substring( 0, lastLocation );
        }
        return item;
    }
}
EndsWithTest.Main();


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile per Smartphone, Windows Mobile per Pocket PC, Xbox 360

.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.

.NET Framework

Supportato in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supportato in: 3.5, 2.0, 1.0

XNA Framework

Supportato in: 2.0, 1.0

Aggiunte alla community

AGGIUNGI
Mostra:
© 2014 Microsoft