元の文字列のバージョンを次のように返すメソッドを作成しようとしています。元の文字列に表示される各数字 0 ~ 9 は、数字の右側にある文字の出現回数に置き換えられます。したがって、文字列 "a3tx2z" は "attttxzzz" になり、"12x" は "2xxx" になります。文字が続かない数字 (つまり、文字列の末尾) は、何も置き換えられません。
私はコードを書きましたが、それは最初の桁だけで機能し、次の桁では変更されません。
public String blowUp( String str ){
StringBuffer buffer = null;
String toAdd = null;
String toReturnString = null;
if( str.length() == 0 ){
return "no string found";
}else{
for( int count = 0; count < str.length(); count++ ){
char c = str.charAt( count );
if( count == str.length() - 1 ){
if( Character.isDigit( c ) ){
return str.substring( 0, count );
}else{
return str;
}
}else if( Character.isDigit( c ) ){
char next = str.charAt( count + 1 );
buffer = new StringBuffer();
int nooftimes = Integer.parseInt(Character.toString( c ));
for( int j = 0; j < nooftimes; j++ ){
buffer.append( next );
}
toAdd = buffer.toString();
toReturnString = str.substring( 0, count ) + toAdd + str.substring( count + 1 );
return toReturnString;
}
}
return toReturnString;
}
// return toReturnString;
}