|
| 1 | +### Aşağıdaki örnekte changeCase metodu içerisinde sürekli String nesnesi yaratılmaktadır |
| 2 | +```java |
| 3 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 4 | + Aşağıdaki örnekte changeCase metodu içerisinde sürekli String nesnesi yaratılmaktadır |
| 5 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 6 | +package org.csystem.app; |
1 | 7 |
|
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +class App { |
| 11 | + public static void main(String [] args) |
| 12 | + { |
| 13 | + Scanner kb = new Scanner(System.in); |
| 14 | + System.out.print("Bir yazı giriniz:"); |
| 15 | + String s = kb.nextLine(); |
| 16 | + |
| 17 | + System.out.println(Util.changeCase(s)); |
| 18 | + |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class Util { |
| 23 | + public static String changeCase(String s) |
| 24 | + { |
| 25 | + int length = s.length(); |
| 26 | + String str = ""; |
| 27 | + |
| 28 | + for (int i = 0; i < length; ++i) { |
| 29 | + char ch = s.charAt(i); |
| 30 | + |
| 31 | + if (Character.isUpperCase(ch)) |
| 32 | + ch = Character.toLowerCase(ch); |
| 33 | + else if (Character.isLowerCase(ch)) |
| 34 | + ch = Character.toUpperCase(ch); |
| 35 | + |
| 36 | + str += ch; //str = str + ch; |
| 37 | + } |
| 38 | + |
| 39 | + return str; |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | +### Yukarıdaki örnekte metot dizi kullanarak daha etkin bir biçimde yazılabilir |
| 44 | +```java |
| 45 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 46 | + Yukarıdaki örnekte metot dizi kullanarak daha etkin bir biçimde yazılabilir |
| 47 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 48 | +package org.csystem.app; |
| 49 | + |
| 50 | +import java.util.Scanner; |
| 51 | + |
| 52 | +class App { |
| 53 | + public static void main(String [] args) |
| 54 | + { |
| 55 | + Scanner kb = new Scanner(System.in); |
| 56 | + System.out.print("Bir yazı giriniz:"); |
| 57 | + String s = kb.nextLine(); |
| 58 | + |
| 59 | + System.out.println(Util.changeCase(s)); |
| 60 | + |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class Util { |
| 65 | + public static String changeCase(String s) |
| 66 | + { |
| 67 | + char [] c = s.toCharArray(); |
| 68 | + |
| 69 | + for (int i = 0; i < c.length; ++i) { |
| 70 | + if (!Character.isLetter(c[i])) |
| 71 | + continue; |
| 72 | + |
| 73 | + c[i] = Character.isUpperCase(c[i]) ? Character.toLowerCase(c[i]) : Character.toUpperCase(c[i]); |
| 74 | + } |
| 75 | + |
| 76 | + return new String(c); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | +### StringBuilder sınıfının default ctor'u capacity değerini 16'ya çeker |
| 81 | +```java |
| 82 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 83 | + StringBuilder sınıfının default ctor'u capacity değerini 16'ya çeker |
| 84 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 85 | +package org.csystem.app; |
| 86 | + |
| 87 | +class App { |
| 88 | + public static void main(String [] args) |
| 89 | + { |
| 90 | + StringBuilder sb = new StringBuilder(); |
| 91 | + |
| 92 | + System.out.printf("Capacity:%d%n", sb.capacity()); |
| 93 | + System.out.printf("Length:%d%n", sb.length()); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | +### StringBuilder sınıfının capacity parametreli ctor'u |
| 98 | +```java |
| 99 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 100 | + StringBuilder sınıfının capacity parametreli ctor'u |
| 101 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 102 | +package org.csystem.app; |
| 103 | + |
| 104 | +class App { |
| 105 | + public static void main(String [] args) |
| 106 | + { |
| 107 | + StringBuilder sb = new StringBuilder(100); |
| 108 | + |
| 109 | + System.out.printf("Capacity:%d%n", sb.capacity()); |
| 110 | + System.out.printf("Length:%d%n", sb.length()); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | +### StringBuilder sınıfının String parametreli ctor'u |
| 115 | +```java |
| 116 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 117 | + StringBuilder sınıfının String parametreli ctor'u |
| 118 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 119 | +package org.csystem.app; |
| 120 | + |
| 121 | +class App { |
| 122 | + public static void main(String [] args) |
| 123 | + { |
| 124 | + StringBuilder sb = new StringBuilder("ankara"); |
| 125 | + |
| 126 | + System.out.printf("Capacity:%d%n", sb.capacity()); |
| 127 | + System.out.printf("Length:%d%n", sb.length()); |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | +### StringBuilder sınıfının append ve toString metotları |
| 132 | +```java |
| 133 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 134 | + StringBuilder sınıfının append ve toString metotları |
| 135 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 136 | +package org.csystem.app; |
| 137 | + |
| 138 | +class App { |
| 139 | + public static void main(String [] args) |
| 140 | + { |
| 141 | + StringBuilder sb = new StringBuilder("ankara"); |
| 142 | + |
| 143 | + sb.append('-').append("istanbul"); |
| 144 | + |
| 145 | + String str = sb.toString(); |
| 146 | + |
| 147 | + System.out.println(str); |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | +### Sınıf Çalışması: Klavyeden quit girilene kadar alınan yazıların birleşiminden bir String elde eden programı yazınız |
| 152 | +```java |
| 153 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 154 | + Sınıf Çalışması: Klavyeden quit girilene kadar alınan yazıların birleşiminden |
| 155 | + bir String elde eden programı yazınız |
| 156 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 157 | +package org.csystem.app; |
| 158 | + |
| 159 | +import org.csystem.util.Console; |
| 160 | + |
| 161 | +class App { |
| 162 | + public static void main(String [] args) |
| 163 | + { |
| 164 | + Console.writeLine("Yazıları girmeye başlayınız:"); |
| 165 | + StringBuilder sb = new StringBuilder(); |
| 166 | + |
| 167 | + for (;;) { |
| 168 | + String s = Console.readString("Bir yazı giriniz:"); |
| 169 | + |
| 170 | + if (s.equals("quit")) |
| 171 | + break; |
| 172 | + |
| 173 | + sb.append(s); |
| 174 | + } |
| 175 | + |
| 176 | + String str = sb.toString(); |
| 177 | + |
| 178 | + Console.writeLine("Yazı:%s", str); |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | +### StringBuilder sınıfının charAt metodu |
| 183 | +```java |
| 184 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 185 | + StringBuilder sınıfının charAt metodu |
| 186 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 187 | +package org.csystem.app; |
| 188 | + |
| 189 | +import org.csystem.util.Console; |
| 190 | + |
| 191 | +class App { |
| 192 | + public static void main(String [] args) |
| 193 | + { |
| 194 | + StringBuilder sb = new StringBuilder("ankara"); |
| 195 | + int length = sb.length(); |
| 196 | + |
| 197 | + for (int i = 0; i < length; ++i) |
| 198 | + Console.write("%c ", sb.charAt(i)); |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | +### StringBuilder kullanılarak addAllCharsWith ve subtractAllCharsWith metotları |
| 203 | +```java |
| 204 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 205 | + StringBuilder kullanılarak addAllCharsWith ve subtractAllCharsWith metotları |
| 206 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 207 | +package org.csystem.app; |
| 208 | + |
| 209 | +import org.csystem.util.Console; |
| 210 | +import org.csystem.util.StringUtil; |
| 211 | + |
| 212 | +class App { |
| 213 | + public static void main(String [] args) |
| 214 | + { |
| 215 | + String str = Console.readString("Bir yazı giriniz:"); |
| 216 | + int n = Console.readInt("n değerini giriniz:", "Hatalı giriş yaptınız:"); |
| 217 | + |
| 218 | + String newStr = StringUtil.addAllCharsWith(str, n); |
| 219 | + String oldStr = StringUtil.subtractAllCharsWith(newStr, n); |
| 220 | + |
| 221 | + Console.writeLine("New String:%s", newStr); |
| 222 | + Console.writeLine("Old String:%s", oldStr); |
| 223 | + } |
| 224 | +} |
| 225 | +``` |
| 226 | +### StringBuilder sınıfının delete metodu |
| 227 | +```java |
| 228 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 229 | + StringBuilder sınıfının delete metodu |
| 230 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 231 | +package org.csystem.app; |
| 232 | + |
| 233 | +import org.csystem.util.Console; |
| 234 | +import org.csystem.util.StringUtil; |
| 235 | + |
| 236 | +class App { |
| 237 | + public static void main(String [] args) |
| 238 | + { |
| 239 | + String str = "ankara"; |
| 240 | + int start = 1; |
| 241 | + int end = 3; |
| 242 | + |
| 243 | + StringBuilder sb = new StringBuilder(str); //[start, end) |
| 244 | + |
| 245 | + str = sb.delete(start, end).toString(); |
| 246 | + |
| 247 | + Console.writeLine("%s", str); |
| 248 | + } |
| 249 | +} |
| 250 | +``` |
| 251 | +### StringBuilder sınıfının reverse metodu kullanarak yazılan StringUtil sınıfının reverse metodu |
| 252 | +```java |
| 253 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 254 | + StringBuilder sınıfının reverse metodu kullanarak yazılan StringUtil |
| 255 | + sınıfının reverse metodu |
| 256 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 257 | +package org.csystem.app; |
| 258 | + |
| 259 | +import org.csystem.util.Console; |
| 260 | +import org.csystem.util.StringUtil; |
| 261 | + |
| 262 | +class App { |
| 263 | + public static void main(String [] args) |
| 264 | + { |
| 265 | + String s = Console.readString("Bir yazı giriniz:"); |
| 266 | + |
| 267 | + Console.writeLine("%s", StringUtil.reverse(s)); |
| 268 | + } |
| 269 | +} |
| 270 | +``` |
| 271 | +### StringUtil sınıfının changeCase metodu |
| 272 | +```java |
| 273 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 274 | + StringUtil sınıfının changeCase metodu |
| 275 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 276 | +package org.csystem.app; |
| 277 | + |
| 278 | +import org.csystem.util.Console; |
| 279 | +import org.csystem.util.StringUtil; |
| 280 | + |
| 281 | +class App { |
| 282 | + public static void main(String [] args) |
| 283 | + { |
| 284 | + String s = Console.readString("Bir yazı giriniz:"); |
| 285 | + |
| 286 | + System.out.println(StringUtil.changeCase(s)); |
| 287 | + } |
| 288 | +} |
| 289 | +``` |
| 290 | +### StringUtil sınıfının getRandomText metodu |
| 291 | +```java |
| 292 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 293 | + StringUtil sınıfının getRandomText metodu |
| 294 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 295 | +package org.csystem.app; |
| 296 | + |
| 297 | +import org.csystem.util.Console; |
| 298 | +import org.csystem.util.StringUtil; |
| 299 | + |
| 300 | +import java.util.Random; |
| 301 | + |
| 302 | +class App { |
| 303 | + public static void main(String [] args) |
| 304 | + { |
| 305 | + int n = Console.readInt("Bir sayı giriniz:"); |
| 306 | + Random r = new Random(); |
| 307 | + |
| 308 | + Console.writeLine(StringUtil.getRandomTextTR(r, n)); |
| 309 | + Console.writeLine(StringUtil.getRandomTextEN(r, n)); |
| 310 | + } |
| 311 | +} |
| 312 | + |
| 313 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 314 | + |
| 315 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 316 | +``` |
| 317 | +```java |
0 commit comments