Notice
Recent Posts
Recent Comments
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
10-10 22:21
Archives
Today
Total
관리 메뉴

Developer_Neo

각종 함수들 본문

프로그래밍/Java

각종 함수들

_Neo_ 2022. 7. 7. 17:41
반응형

문자열 비교  - ___.equals(~~~)

public class Example{
	public static void main(){
    	String str ="";
        if(str.equals("")) System.out.println("empty value");
    }
}

 

 

문자열 길이  - ___.length()

public class Example{
	public static void main(){
    	String word ="random";
        System.out.println(word.length());  // 출력결과 : 6
    }
}

 

 

문자열 소문자/ 대문자로 변경  - ___.toLowerCase()  /  ___.toUpperCase()

public class Example{
	public static void main(){
    	String word ="RanDom";
        System.out.println(word.toLowerCase() + word.toUpperCase());  
        // 출력결과 : random  RANDOM
    }
}

 

* 문자열은 변경이 불가 따라서 몇가지를 바꾼 문자열이 필요하다면 새로운 문자열을 만들어서 + 연산으로 진행하면 된다. 

 

 

특정 문자열  뽑기 - ___.substring(시작인덱스값, 마지막 인덱스값+1)

public class Example{
	public static void main(){
    	String word ="random";
        System.out.println(word.substring(1,word.length()-1);  // 출력결과 : ando
    }
}

 

문자열 잘라 배열로 저장  - ___.split( 구분자 )

public class Example{
	public static void main(){
    	String word ="random is not good";
        // String[] arr=word.split(" ");
        System.out.println(word.split(" "));  // 출력결과 : ["random", "is", "not", "good"]
    }
}

 

문자열 중 특정 문자열로 변경   - ___.replace(변경 전 문자열,변경 후 문자열

                                                          ___.replaceAll(변경 전 문자열,변경 후 문자열 )

 

public class Example{
	public static void main(){
    	String str = "aaabbbccccabcddddabcdeeee"
        String result1 = str.replace("abc", "응");
        String result2 = str.replaceAll("[abc]", "응");
        //위와 같은 결과는 str.replace("a", "응").replace("b", "응").replace("c", "응");
        System.out.println("   replace result->"+ result1);
        //aaabbbcccc응dddd응deeee
  		System.out.println("replaceAll result->"+ result2);
        // 응응응응응응응응응응응응응dddd응응응deeee
    }
}

replaceAll은 정규식표현을 사용한다는 특징이 있다.


소수점 내림연산  -  Math.floor(숫자)  -> 반환값은 double형 or int형 이다.

public class Example{
	public static void main(){
        System.out.println((int)Math.floor((8+9)/2));  // 출력결과 : 8
    }
}

 

절댓값  -  Math.abs(숫자)  

public class Example{
	public static void main(){
        System.out.println((Math.abs(-8));  // 출력결과 : 8
    }
}

 


문자를 int로 바꾸기  '어떤 문자' - '0' Or  Character.getNumericValue(문자)

public class Example{
	public static void main(){
    	int result=Character.getNumericValue('9');
        System.out.println(result);
    }
}

그냥 (int)로 형변환 해주면 아스키코드값에 해당하는 것으로 반환한다. (char)형변환도 마찬가지이다.


int를 문자로 바꾸기  Character.forDigit(문자, 몇진수인지) Or 어떤 숫자 + '0'

public class Example{
	public static void main(){
    	char result=Character.forDigit(10,10);
        // char result = result = (char) (num + '0');
        System.out.println(result);
    }
}

int를 문자열로 바꾸기  Integer.toString(숫자) 

public class Example{
	public static void main(){
    	String result=Integer.toString(1);
        result+=Integer.toString(2);
        System.out.println(result);  //출력결과 : 12
    }
}

String타입의 문자열에서 한가지 문자(char)타입의 값을 사용

- 문자열변수.charAt(문자열인덱스)

public class Example{
	public static void main(){
    	String result="abcdefg";
        System.out.println(result.charAt(0));  //출력결과 : a
    }
}

 


배열 복사  - Arrays.copyOf(복사할 배열, 복사할 길이)  Or

                     System.arraycopy(복사할 배열, 복사시작위치,  복사한걸 저장할 배열, 저장시작위치, 길이(이길이만큼 복사))  Or

                       Arrays.copyOfRange(복사할 배열, 시작위치, 끝날위치)

public class Example{
	public static void main(){
    	int[] arr={1, 2, 3, 4, 5};
        int[] nums=Arrays.copyOf(arr,arr.length+1);
        for(int i: nums)
        	System.out.print(i); 
        //출력결과 : 123450      
    }
}


// --------------------------------------------------------------------------------------
public class Example{
	public static void main(){
    	int[] arr={1, 2, 3, 4, 5};
        int[] num = new int[arr.length+1];
        num[0]=99;
    	System.arraycopy(arr,0,num,1,arr.length);
        for(int i: nums)
        	System.out.print(i); 
        //출력결과 : 9912345

    }
}

// --------------------------------------------------------------------------------------
public class Example{
	public static void main(){
    	int[] arr={1, 2, 3, 4, 5};
        int[] num = Arrays.copyOfRange(arr, 0, 3);
        for(int i: nums)
        	System.out.print(i); 
        //출력결과 : 123

    }
}

길이를 1늘려서 복사를 했는데 빈배열을 생성할 때와 마찬가지로 할당이 되지 않은 것은 0으로 저장되어있다.

 

반응형
Comments