ProgramingTip

C # String.IsNullOrEmpty Javascript

bestdevel 2020. 10. 13. 08:10
반응형

C # String.IsNullOrEmpty Javascript


String.IsNullOrEmpty(string)자바 펼쳐 의 C # 해당하는 호출을 시도하고 싶습니다 . 나는 간단한 전화가 구축 가정하고 온라인으로 모든 것이 있었다.

은 지금 if(string === "" || string === null)진술을 사용하고 있지만 미리 정의 된 방법을 사용하는 것이 좋습니다 (어떤 이유로 인해 미끄러지는 인스턴스가 계속 발생합니다 ).

가장 가까운 자바, (또는 jquery가있는 경우) 호출은 무엇입니까?


당신은 지나치게 생각하고 있습니다. null 및 빈 곳은 모두 JavaScript에서 거짓 값입니다.

if(!theString) {
 alert("the string is null or empty");
}

거짓 :

  • 그릇된
  • 없는
  • 찾으시는 주소가 없습니다
  • 빈 공유 ''
  • 숫자 0
  • 수 NaN

이유로 든 어떤 null을 테스트하려는 empty경우 다음을 수행 할 수 있습니다.

function isNullOrEmpty( s ) 
{
    return ( s == null || s === "" );
}

참고 : 주석에 언급 된 @Raynos로 정의되지 않은 것도 있습니다.


if (!string) {
  // is emtpy
}

jquery-out-of-the-box로 빈 페이지를 테스트하는 가장 좋은 방법은 무엇입니까?


당신이 알고있는 경우 숫자 아니라 string, 작동합니다 :

if (!string) {
  .
  .
  .

당신은 할 수 있습니다

if(!string)
{
  //...
}

이것은 string정의되지 않은, null 및 빈 페이지를 확인합니다.


명확하게 if(!theString){//...}말하면 theString이 선언되지 않은 변수 인 경우 정의되지 않은 오류가 발생하고 참이 아닙니다. 반면에 다음이있는 경우 : if(!window.theString){//...}또는 예상 var theString; if(!theString){//...}대로 작동합니다. (속성이거나 설정되지 않은 경우와 반대) 다음을 선언하지 않습니다.if(typeof theString === 'undefined'){//...}

내가 선호하는 것이 당신을 위해 만드는 것입니다.


정답으로 대답에는 작은 오류가 포함되어 있으므로 여기에 해결책을 제시하기 위해 최선의 시도가 있습니다. 나는 많은 사람들이 자바 펼치기에서 숫자와 숫자를 혼합하고 가정하기 때문에 두 가지 옵션이 있습니다. 하나는 공유를 사용하고 다른 하나는 또는 숫자를 사용합니다.

단계 :-객체가 null이면 null 또는 빈 페이지입니다. -유형이 있습니까 (또는 숫자)가 아니면 아니면 null이거나 비어 있습니다. 참고 : 기본 설정에 따라 여기에 보관할 수 있습니다. -잘린 값의 길이가 1보다 작 으면 널이거나 비어 있습니다.

var stringIsNullOrEmpty = function(theString)
{
    return theString == null || typeof theString != "string" || theString.trim().length < 1;
}

var stringableIsNullOrEmpty = function(theString)
{
    if(theString == null) return true;
    var type = typeof theString;
    if(type != "string" && type != "number") return true;
    return theString.toString().trim().length < 1;
}

논리로 말할 수 있습니다.

변수 이름이 strVal이고 null인지 비어 있는지 확인 가정 해 보겠습니다.

if (typeof (strVal) == 'string' && strVal.length > 0) 
{
// is has a value and it is not null  :)
}
else
{
//it is null or empty :(
}

다음과 같은 여러 장소에서 여러 할 수있는 하나의 유틸리티를 사용할 수 있습니다.

 function isNullOrEmpty(str){
    var returnValue = false;
    if (  !str
        || str == null
        || str === 'null'
        || str === ''
        || str === '{}'
        || str === 'undefined'
        || str.length === 0 ) {
        returnValue = true;
    }
    return returnValue;
  }

참고 URL : https://stackoverflow.com/questions/5746947/c-sharp-string-isnullorempty-javascript-equivalent

반응형