SORU
13 Temmuz 2011, ÇARŞAMBA


Bölü çizgisi ile bitmeyen bir dönüş dize

İki değişken var:

site1 = "www.somesite.com";  
site2 = "www.somesite.com/";  

Böyle bir şey yapmak istiyorum

function someFunction(site)
{
    // If the var has a trailing slash (like site2), 
    // remove it and return the site without the trailing slash
    return no_trailing_slash_url;
}

Bunu nasıl yapabilirim?

CEVAP
13 Temmuz 2011, ÇARŞAMBA


function stripTrailingSlash(str) {
    if(str.substr(-1) === '/') {
        return str.substr(0, str.length - 1);
    }
    return str;
}

Not: ve eski olumsuz substr uzaklıklar desteklemez IE8. Eğer bu eski tarayıcıları desteklemek gerekiyorsa yerine str.length - 1 kullanın.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Kanaal van Dj0fifty

    Kanaal van D

    28 EKİM 2011
  • Flash CS6 Video Tutorials for Beginners (Actionscript 3 Gaming)

    Flash CS6 Vi

    14 EYLÜL 2012
  • TeachMeComputer

    TeachMeCompu

    31 EKİM 2009