java版本:
//stringbuffer可变字符串,substring截取字符串
class Solution {
public String reversePrefix(String word, char ch) {
int index=0;
for(int i=0;i<word.length();i++){
if(word.charAt(i)==ch){
index=i;
break;
}
}
StringBuffer str=new StringBuffer(word.substring(0,index+1));
str.reverse();
str.append(word.substring(index+1));
return str.toString();
}
}
网友评论