program tip

문자열이 null인지 비어 있는지 확인하는 가장 쉬운 방법

radiobox 2020. 9. 16. 07:33
반응형

문자열이 null인지 비어 있는지 확인하는 가장 쉬운 방법


비어 있거나 null 문자열을 확인하는 코드가 있습니다. 테스트 중입니다.

eitherStringEmpty= (email, password) ->
  emailEmpty = not email? or email is ''
  passwordEmpty = not password? or password is ''
  eitherEmpty = emailEmpty || passwordEmpty         

test1 = eitherStringEmpty "A", "B" # expect false
test2 = eitherStringEmpty "", "b" # expect true
test3 = eitherStringEmpty "", "" # expect true
alert "test1: #{test1} test2: #{test2} test3: #{test3}"

내가 궁금한 것은 not email? or email is ''. string.IsNullOrEmpty(arg)한 번의 호출로 CoffeeScript에서 C # 해당하는 작업을 수행 할 수 있습니까 ? 나는 항상 (내가했던 것처럼) 함수를 정의 할 수 있지만, 내가 놓친 언어가 있는지 궁금하다.


예:

passwordNotEmpty = not not password

이하 :

passwordNotEmpty = !!password

완전히 동일하지는 않지만 null이 email?.length아니고 속성 email이 0이 아닌 경우에만 진실 .length합니다. 당신이 만약 not당신이 문자열과 배열을 모두 원하는대로이 값은 결과는 행동해야한다.

경우 email입니다 null또는이없는 .length한 다음 email?.length에 평가합니다 nullfalsey이다. 가있는 .length경우이 값은 길이로 평가되며 비어 있으면 거짓이됩니다.

함수는 다음과 같이 구현 될 수 있습니다.

eitherStringEmpty = (email, password) ->
  not (email?.length and password?.length)

이것은 "진실"이 도움이되는 경우입니다. 이를 위해 함수를 정의 할 필요조차 없습니다.

test1 = not (email and password)

왜 작동합니까?

'0'       // true
'123abc'  // true
''        // false
null      // false
undefined // false

unless email? and email
  console.log 'email is undefined, null or ""'

먼저 이메일이 정의되지 않았고 존재 연산자로 null이 아닌지 확인한 다음 and email이메일 문자열이 비어있는 경우에만 해당 부분이 false를 반환합니다.


coffeescript 또는 = 작업을 사용할 수 있습니다.

s = ''    
s or= null

콘텐츠가 null이 아니고 배열이 아닌 문자열인지 확인해야하는 경우 간단한 비교 유형을 사용합니다.

 if typeof email isnt "string"

다음은 이를 수행하는 매우 쉬운 방법을 보여주는 jsfiddle 입니다.

기본적으로 이것은 자바 스크립트입니다.

var email="oranste";
var password="i";

if(!(email && password)){
    alert("One or both not set");        
}
else{
    alert("Both set");   
}

coffescript에서 :

email = "oranste"
password = "i"
unless email and password
  alert "One or both not set"
else
  alert "Both set"

이것이 누군가를 돕기를 바랍니다 :)


나는 물음표가 사물이 존재하는 경우 함수를 호출하는 가장 쉬운 방법이라고 생각합니다.

예를 들면

car = {
  tires: 4,
  color: 'blue' 
}

색상을 얻고 싶지만 차가있는 경우에만 ...

coffeescript :

 car?.color

javascript로 번역 :

if (car != null) {
  car.color;
}

실존 연산자 http://coffeescript.org/documentation/docs/grammar.html#section-63 이라고합니다 .


@thejh의 답변이 빈 문자열을 확인하기에 충분하다고 확신하지만 '존재합니까?'라는 것을 자주 확인해야한다고 생각합니다. 그런 다음 '비어 있습니까? 문자열, 배열 및 객체 포함 '

이것은 CoffeeScript가이를 수행하는 짧은 방법입니다.

tmp? and !!tmp and !!Object.keys(tmp).length

If we keep this question order, that would be checked by this order 1. does it exist? 2. not empty string? 3. not empty object?

so there wasn't any problems for all variable even in the case of not existed.


Based on this answer about checking if a variable has a truthy value or not , you just need one line:

result = !email or !password

& you can try it for yourself on this online Coffeescript console


Instead of the accepted answer passwordNotEmpty = !!password you can use

passwordNotEmpty = if password then true else false

It gives the same result (the difference only in syntax).

In the first column is a value, in the second is the result of if value:

0 - false
5 - true
'string' - true
'' - false
[1, 2, 3] - true
[] - true
true - true
false - false
null - false
undefined - false

참고URL : https://stackoverflow.com/questions/8127883/easiest-way-to-check-if-string-is-null-or-empty

반응형