보통 if문으로 문자열을 비교 할때

String TEST = "java";

 

if(TEET.equals("java")){

......

}

 

 

요런식으로 비교를 합니다. 하지만 이렇게 비교 하면 TEST에 null 값이 들어 가면 에러 나게 됩니다. 하지만

 

 

if("java".euals(TEST)){

....

}else{

....

}

 

if( srt != null && str.equals("java") ){

}

 

출처 : 코드인

[출처] if문 문자열 비교|작성자 세바니

F5 - 함수 안으로

F6 - 다음함수

F8 - 컴파일로 복귀 (완료, 다음 중단점에 다시걸림)

String 변환
    public String convertStreamToString(InputStream is)
            throws IOException {
        /*
         * To convert the InputStream to String we use the
         * Reader.read(char[] buffer) method. We iterate until the
         * Reader return -1 which means there's no more data to
         * read. We use the StringWriter class to produce the string.
         */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {        
            return "";
        }
    }
StringBuffer 변환
     StringBuffer out = new StringBuffer();
     byte[] b = new byte[4096];
     for (int n; (n = inputStream.read(b)) != -1;) {
         out.append(new String(b, 0, n));
     }
     out.toString();


+ Recent posts