StringUtilReview

"Java Stuty"

Posted by Chungman on March 19, 2021
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package HomeWorkReview;

public class StringUtilReview {

	/**
	 * 문자열을 거꾸로 변환하는 메소드
	 */
	public String reverseString(String str) {
		
		int length = str.length();
		char [] chars = new char[length];
		str.getChars(0, length, chars, 0);	// 값 복사
		
		for(int i = 0, j = length; i < length/2; i++) {
			char temp = chars[i];
			chars[i] = chars[j];
			chars[j] = temp;
		}
		return new String(chars);
		
		/*
		StringBuffer sb = new StringBuffer();
		String revStr = new String();
		for(int i = str.length()-1, j = 0; i >= 0; i--) {
			sb.append(str.charAt(i));
		}
		
		return sb.toString();
		*/
		
		/*
		char[] chars = new char[str.length()];
		for(int i = str.length()-1, j = 0; i >= 0; i--) {
			chars[j++] = str.charAt(i);
		}
		
		return new String(chars);
		*/
	}
	
	/**
	 * 문자열을 대문자로 변경하는 메소드
	 */
	public String toUpperString(String str) {
		
		char[] chars = new char[str.length()];
		str.getChars(0, str.length(), chars, 0);
		
		for(int i = 0; i < chars.length; i++) {			// chars.length()가 아닌 이유 : chars의 형태는 배열
			if(chars[i] >= 'a' && chars[i] <= 'z')
				chars[i] -= ('a'-'A');
		}
		return new String(chars);
		
		/*
		byte[] bytes = new byte[str.length()];
		
		for(int i = 0; i < str.length(); i++) {
			
			byte b = (byte)str.charAt(i);
			// 소문자의 경우 대문자로 변경해서 대입
			if(b >= 'a' && b <= 'z')
				b = (byte)(b - ('a'-'A'));
			bytes[i] = b;
		}
		
		return new String(bytes);
		*/
	}
}