回答編集履歴
1
改修点の補足
answer
CHANGED
@@ -2,4 +2,126 @@
|
|
2
2
|
|
3
3
|
上記メソッドは他のメソッドの改善により解決しました。
|
4
4
|
|
5
|
-
次からはもう少し質問方法を勉強してから質問するようにいたします。
|
5
|
+
次からはもう少し質問方法を勉強してから質問するようにいたします。
|
6
|
+
|
7
|
+
|
8
|
+
間違っていたところはisEmptyメソッドのif文returnがtrue、false逆になっていたためでした。
|
9
|
+
そのためすべて値がisPaddableの入力チェックでreturn targetとなってしまい処理をせずに終了していたため変化が何も起こらなかったということでした。
|
10
|
+
|
11
|
+
説明や質問の仕方が悪くご迷惑おかけして申し訳ありません。
|
12
|
+
|
13
|
+
'''
|
14
|
+
//文字列を指定サイズで中央寄せするメソッドを作成してください。
|
15
|
+
//※作成済みのメソッド"leftPad", "rightPad" を参考にしてください。
|
16
|
+
public static String toCenter(String target, int byt, String str) {
|
17
|
+
String hoge = "";
|
18
|
+
|
19
|
+
if (target == null) {
|
20
|
+
return target = "";
|
21
|
+
}
|
22
|
+
if (str == null) {
|
23
|
+
return target = "";
|
24
|
+
}
|
25
|
+
int n = getBytesAsShiftJIS(str);
|
26
|
+
|
27
|
+
//getBYtesASSHIftJIseメソッドは引数のバイト数を返すメソッド
|
28
|
+
|
29
|
+
hoge = rightPad(target, (byt + n - getBytesAsShiftJIS(target)) / 2 + getBytesAsShiftJIS(target), str);
|
30
|
+
hoge = leftPad(hoge, byt + 1, str);
|
31
|
+
|
32
|
+
return hoge;
|
33
|
+
|
34
|
+
}
|
35
|
+
|
36
|
+
public static String substringBytes(String target, int beginIndex, int endIndex) {
|
37
|
+
StringBuilder result = new StringBuilder();
|
38
|
+
int size = 0;
|
39
|
+
for (char chr : target.toCharArray()) {
|
40
|
+
size += getBytesAsShiftJIS(chr);
|
41
|
+
if (size < endIndex) {
|
42
|
+
if (beginIndex <= size) {
|
43
|
+
result.append(chr);
|
44
|
+
}
|
45
|
+
} else {
|
46
|
+
break;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
return result.toString();
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
// 対象文字列の左側に指定された文字列を詰めて指定サイズに整形する
|
55
|
+
public static String leftPad(String target, int size, char padChar) {
|
56
|
+
return leftPad(target, size, String.valueOf(padChar));
|
57
|
+
}
|
58
|
+
|
59
|
+
// 対象文字列の左側に指定された文字列を詰めて指定サイズに整形する
|
60
|
+
public static String leftPad(String target, int size, String padString) {
|
61
|
+
// 入力チェック
|
62
|
+
if (!isPaddable(target, size, padString)) {
|
63
|
+
return target;
|
64
|
+
}
|
65
|
+
|
66
|
+
int padSize = size - getBytesAsShiftJIS(target);
|
67
|
+
return substringBytes(
|
68
|
+
padString.repeat((int) Math.ceil((double) padSize / getBytesAsShiftJIS(padString))), 0, padSize)
|
69
|
+
+ target;
|
70
|
+
}
|
71
|
+
|
72
|
+
// 対象文字列の右側に指定された文字列を詰めて指定サイズに整形する
|
73
|
+
public static String rightPad(String target, int size, char padChar) {
|
74
|
+
return rightPad(target, size, String.valueOf(padChar));
|
75
|
+
}
|
76
|
+
|
77
|
+
// 対象文字列の右側に指定された文字列を詰めて指定サイズに整形する
|
78
|
+
public static String rightPad(String target, int size, String padString) {
|
79
|
+
// // 入力チェック
|
80
|
+
if (!isPaddable(target, size, padString)) {
|
81
|
+
return target;
|
82
|
+
}
|
83
|
+
|
84
|
+
int padSize = size - getBytesAsShiftJIS(target);
|
85
|
+
return target
|
86
|
+
+ substringBytes(
|
87
|
+
padString.repeat((int) Math.ceil((double) padSize / getBytesAsShiftJIS(padString))), 0,
|
88
|
+
padSize);
|
89
|
+
}
|
90
|
+
|
91
|
+
// パディング可否判定
|
92
|
+
private static boolean isPaddable(String target, int size, String padString) {
|
93
|
+
return !isEmpty(target)
|
94
|
+
&& 0 < size
|
95
|
+
&& !isEmpty(padString)
|
96
|
+
&& 0 < size - target.length();
|
97
|
+
//return false;
|
98
|
+
}
|
99
|
+
|
100
|
+
}
|
101
|
+
|
102
|
+
public static boolean isEmpty(String target) {
|
103
|
+
|
104
|
+
if (target == null || target.matches("")) {
|
105
|
+
|
106
|
+
return true;
|
107
|
+
}
|
108
|
+
return false;
|
109
|
+
|
110
|
+
}
|
111
|
+
'''
|
112
|
+
|
113
|
+
実行するコード
|
114
|
+
'''
|
115
|
+
writer.println("Util.toCenter(\"1234\", 20, null) ==> " + Util.toCenter("1234", 20, null));
|
116
|
+
writer.println("Util.toCenter(\"1234\", 20, \"\") ==> " + Util.toCenter("1234", 20, ""));
|
117
|
+
writer.println("Util.toCenter(null, 20, \"+\") ==> " + Util.toCenter(null, 20, "+"));
|
118
|
+
writer.println("Util.toCenter(\"\", 20, \"+\") ==> " + Util.toCenter("", 20, "+"));
|
119
|
+
writer.println("Util.toCenter(\"1234\", 20, \"*-\") ==> " + Util.toCenter("1234", 20, "*-"));
|
120
|
+
writer.println("Util.toCenter(\"12345\", 20, \"*-\") ==> " + Util.toCenter("12345", 20, "*-"));
|
121
|
+
writer.println("Util.toCenter(\"1234\", 20, \"??\") ==> " + Util.toCenter("1234", 20, "??"));
|
122
|
+
writer.println("Util.toCenter(\"1234\", 20, \"??\") ==> " + Util.toCenter("1234", 20, "??"));
|
123
|
+
writer.println("Util.toCenter(\"$$$$\", 20, \"??\") ==> " + Util.toCenter("$$$$", 20, "??"));
|
124
|
+
writer.println("Util.toCenter(\"$123\", 20, \"??\") ==> " + Util.toCenter("$123", 20, "??"));
|
125
|
+
|
126
|
+
|
127
|
+
'''
|