2017-01-30 2 views
-1

비즈니스 로직 레이어 (C#)에 유창하지만 javscript로 녹슨 것입니다.URL 끝에 추가/

"/"로 끝나지 않는 경우에만 문자열의 끝에 "/"를 추가하는 더 안전한 방법은 무엇입니까?

현재, 내가 가진 :

  if (entityId != undefined) { 
       noteUrl = window.parent.serverUrl; 
       if (noteUrl.substring(noteUrl.length - 1) != "/") { 
        noteUrl += "/"; 
       } 
+4

['경우 (! noteUrl.endsWith ("/")) {...'] (https://developer.mozilla.org/ 유용 en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) "안전하다"는 것이 무슨 뜻인지 모르겠지만 조금 더 좋다고 생각합니다. –

답변

1

삼항 표현을 사용 : true 또는 false 부울을 반환 endsWith

noteUrl += noteUrl.endsWith("/") ? "" : "/"; 
관련 문제