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

    stringhe - i primi 4 caratteri da destra o sinistra

    ciao, c'è modo per prelevare da una stringa i primi n caratteri partendo da destra?

    ad esempio

    stringR("dasdsakjhdaskpincopallo",4) mi restituisce "allo"

    stringL("ciaosakjhdaskpincopallo",4) mi restituisce "ciao"
    Sono così maledettamente attraente...

    QuickChat. (Asp e ajax)

  2. #2
    Utente di HTML.it L'avatar di floyd
    Registrato dal
    Apr 2001
    Messaggi
    3,837
    var s = "dasdsakjhdaskpincopallo";
    var right = s.substring(s.length-4);

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    trovate in rete il secolo scorso
    codice:
    /*------------------------------------------------------------------------
    //
    -------------------------------------------------------------------------*/
    function Right(str, n)
            /***
                    IN: str - the string we are RIGHTing
                        n - the number of characters we want to return
    
                    RETVAL: n characters from the right side of the string
            ***/
            {
                    
                    
                    if (n <= 0)     // Invalid bound, return blank string
                       return "";
                    else if (n > String(str).length)   // Invalid bound, return
                       return str;                     // entire string
                    else { // Valid bound, return appropriate substring
                       var iLen = String(str).length;
                       return String(str).substring(iLen, iLen - n);
                    }
            }
    
    
    
    /*------------------------------------------------------------------------
    //
    -------------------------------------------------------------------------*/
    function Left(str, n)
            /***
                    IN: str - the string we are LEFTing
                        n - the number of characters we want to return
    
                    RETVAL: n characters from the left side of the string
            ***/
            {
                    if (n <= 0)     // Invalid bound, return blank string
                            return "";
                    else if (n > String(str).length)   // Invalid bound, return
                            return str;                // entire string
                    else // Valid bound, return appropriate substring
                            return String(str).substring(0,n);
            }
    
    
    /*------------------------------------------------------------------------
    //
    -------------------------------------------------------------------------*/
    function Len(str)
            /***
                    IN: str - the string whose length we are interested in
    
                    RETVAL: The number of characters in the string
            ***/
    {  return String(str).length;  }
    
    
    
    /*------------------------------------------------------------------------
    //
    -------------------------------------------------------------------------*/
    function Mid(str, start, len)
    /***
            IN: str - the string we are LEFTing
                start - our string's starting position (0 based!!)
                len - how many characters from start we want to get
    
            RETVAL: The substring from start to start+len
    ***/
    {
            // Make sure start and len are within proper bounds
            if(len == null)
            {
    			return String(str).substr(start);
            }
            else
    			return String(str).substr(start, len);
    			
    }
    
    /*-------------------------------------------------------------------------
    // Keep in mind that strings in JavaScript are zero-based, so if you ask
    // for Mid("Hello",1,1), you will get "e", not "H".  To get "H", you would
    // simply type in Mid("Hello",0,1)
    
    // You can alter the above function so that the string is one-based.  Just
    // check to make sure start is not <= 0, alter the iEnd = start + len to
    // iEnd = (start - 1) + len, and in your final return statement, just
    // return ...substring(start-1,iEnd)
    --------------------------------------------------------------------------*/
    
    function InStr(strSearch, charSearchFor)
    /*
    InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                               was found in the string str.  (If the character is not
                               found, -1 is returned.)
                               
    Requires use of:
    	Mid function
    	Len function
    */
    {
    	for (i=0; i < Len(strSearch); i++)
    	{
    	    if (charSearchFor == Mid(strSearch, i, 1))
    	    {
    			return i;
    	    }
    	}
    	return -1;
    }
    Pietro

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.