Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Javascript funzioni sulle stringhe

    Come posso fare a prendere in considerazione gli ultimi 3 caratteri di una stringa?

    per esempio la stringa

    ciao.gif

    io devo controllare se le ultime 3 lettere sono 'gif'

    Ciao e grazie

  2. #2
    Utente di HTML.it L'avatar di ilgiovo
    Registrato dal
    Aug 2001
    Messaggi
    518
    substring
    Returns a subset of a String object. Method of
    String

    Implemented in
    JavaScript 1.0, NES 2.0

    ECMA version
    ECMA-262



    Syntax
    substring(indexA, indexB)
    Parameters
    indexA
    An integer between 0 and 1 less than the length of the string.

    indexB
    An integer between 0 and 1 less than the length of the string.



    Description
    substring extracts characters from indexA up to but not including indexB. In particular:


    If indexA is less than 0, indexA is treated as if it were 0.

    If indexB is greater than stringName.length, indexB is treated as if it were stringName.length.

    If indexA equals indexB, substring returns an empty string.

    If indexB is omitted, indexA extracts characters to the end of the string.
    In JavaScript 1.2, using LANGUAGE="JavaScript1.2" in the SCRIPT tag,


    If indexA is greater than indexB, JavaScript produces a runtime error (out of memory).
    In JavaScript 1.2, without LANGUAGE="JavaScript1.2" in the SCRIPT tag,


    If indexA is greater than indexB, JavaScript returns a substring beginning with indexB and ending with indexA - 1.
    Examples
    Example 1. The following example uses substring to display characters from the string "Netscape":

    var anyString="Netscape"
    // Displays "Net"document.write(anyString.substring(0,3))docum ent.write(anyString.substring(3,0))// Displays "cap"document.write(anyString.substring(4,7))docum ent.write(anyString.substring(7,4))// Displays "Netscap"document.write(anyString.substring(0, 7))// Displays "Netscape"document.write(anyString.substring(0,8)) document.write(anyString.substring(0,10))
    Example 2. The following example replaces a substring within a string. It will replace both individual characters and substrings. The function call at the end of the example changes the string "Brave New World" into "Brave New Web".

    function replaceString(oldS,newS,fullS) {// Replaces oldS with newS in the string fullS for (var i=0; i<fullS.length; i++) { if (fullS.substring(i,i+oldS.length) == oldS) { fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.l ength,fullS.length) } } return fullS}
    replaceString("World","Web","Brave New World")
    Example 3. In JavaScript 1.2, using LANGUAGE="JavaScript1.2", the following script produces a runtime error (out of memory).

    <SCRIPT LANGUAGE="JavaScript1.2">str="Netscape"document.wr ite(str.substring(0,3);document.write(str.substrin g(3,0);</SCRIPT>
    Without LANGUAGE="JavaScript1.2", the above script prints the following:

    Net Net

    In the second write, the index numbers are swapped.

    See also
    substr



    substr
    Returns the characters in a string beginning at the specified location through the specified number of characters. Method of
    String

    Implemented in
    JavaScript 1.0, NES 2.0



    Syntax
    substr(start[, length])
    Parameters
    start
    Location at which to begin extracting characters.

    length
    The number of characters to extract



    Description
    start is a character index. The index of the first character is 0, and the index of the last character is 1 less than the length of the string. substr begins extracting characters at start and collects length number of characters.

    If start is positive and is the length of the string or longer, substr returns no characters.

    If start is negative, substr uses it as a character index from the end of the string. If start is negative and abs(start) is larger than the length of the string, substr uses 0 is the start index.

    If length is 0 or negative, substr returns no characters. If length is omitted, start extracts characters to the end of the string.

    Example
    Consider the following script:

    <SCRIPT LANGUAGE="JavaScript1.2">
    str = "abcdefghij"document.writeln("(1,2): ", str.substr(1,2))document.writeln("(-2,2): ", str.substr(-2,2))document.writeln("(1): ", str.substr(1))document.writeln("(-20, 2): ", str.substr(1,20))document.writeln("(20, 2): ", str.substr(20,2))
    </SCRIPT>
    This script displays:

    (1,2): bc(-2,2): ij(1): bcdefghij(-20, 2): bcdefghij(20, 2):
    See also
    substring
    Cacca!

  3. #3
    Quindi se non ho capito male dovrei dare la partenza negativa al Substr, esempio substr(-2,2) però non va sigh

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.