クイックソートを実装するなんてのはまずない。あれを一発で通せなんていう無茶な要求がありましたよね。ハノイの塔とか。
複雑であればライブラリを使う。
それでも多少は面倒なときもあります。
文字列を分割するとか。
これ、特定の文字列を変換するコードです。全ての文字列を単変換ならライブラリがありますが、各変換ごと数値を当てはめるとかカスタマイズするとなるとやっぱり書かざるを得ない。
This file contains hidden or 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
public String replaceParam(String str, String[] list) { | |
String res = ""; | |
int index = 0; | |
int count = 0; | |
int prev = 0; | |
while ((index = str.indexOf('?', index)) != -1) { | |
if (count < list.length) { | |
if (prev < index) { | |
res += str.substring(prev, index); | |
} | |
res += list[count]; | |
count++; | |
} | |
index++; | |
prev = index; | |
} | |
if (prev < str.length()) { | |
res += str.substring(prev); | |
} | |
return res; | |
} |