Last active
August 31, 2018 08:19
-
-
Save mrmercc/1262f0fb3315da09acb9c4e0a8a994c5 to your computer and use it in GitHub Desktop.
Array string replacement in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
[EN] | |
Java array string replacement example | |
Easy to use, single function array replacement example. | |
Takes search array lenght and replace it with array index. | |
[TR] | |
Java array ile toplu string karakter değiştirme | |
Kullanımı kolay tek fonksiyonluk bir örnektir. | |
Aranacak karakterleri dizi değişkene (array) alır ve döngüye sokar | |
Aranacak dizi değişkenlerin indexini değiştirilecek dizi değişkenlerin indexiyle eşleştirir | |
ve değiştirilmiş karakterleri string olarak döndürür. | |
@author Ömer Çelik / @mercelikk | |
@version 1.0 | |
*/ | |
package com.example.strArrayReplace; | |
public class Main { | |
/* | |
Main Function | |
*/ | |
public static void main(String[] args) { | |
String str = "ömer çelık"; | |
System.out.println("[ORIGINAL] " + str); | |
String replaced = Main.replaceStrings(str); | |
System.out.println("[REPLACED] " + replaced); | |
} | |
/* | |
Takes the string, loop through array and | |
replace the value with array index | |
@param String str | |
@return String | |
*/ | |
public static String replaceStrings(String str) { | |
// search array | |
// aranacak harfler | |
String[] search = {"ö", "ş", "ğ", "ç", "ı"}; | |
// replace array | |
// değiştirilecek harfler | |
String[] replace = {"o", "s", "g", "c", "i"}; | |
/* | |
[EN] | |
Loop through the array. | |
Do not do index <= search.lnght | |
Throws ArrayIndexOutOfBoundsException | |
See https://stackoverflow.com/a/5554781 for more details | |
Java array reference at Oracle | |
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html | |
[TR] | |
Aranacak karakterleri alır ve döngüye sokar | |
index <= şeklinde yazmayın ! | |
Aksi takdirde ArrayIndexOutOfBoundsException fırlatır | |
Detaylı bilgi için: https://stackoverflow.com/a/5554781 | |
Resmi Java dizi değişkenler referansı: | |
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html | |
*/ | |
for(int index=0; index < search.length; index++) { | |
// replace the string chars with search array index to replace array index | |
str = str.replace(search[index], replace[index]); | |
} | |
// return the replaced string | |
return str; | |
} | |
} | |
// iyi günlerde kullanın (: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment