题1:给定一个字符串、一个起始字符和一个终止字符,找起始字符和终止字符之间的字符串。
public static void main(String[] args) {
String str = "abcdcbaabcdfasdfbadbcbdaaosjfabcs";
String firstChar = "b";
String lastChar = "c";
int firstCharIndex = str.indexOf(firstChar);
int lastCharIndex = str.lastIndexOf(lastChar);
int number = 0;
while (firstCharIndex > -1 && firstCharIndex < str.length()
&& firstCharIndex < lastCharIndex) {
while (firstCharIndex > -1 && firstCharIndex < lastCharIndex
&& lastCharIndex > -1) {
String subStr = str
.substring(firstCharIndex, lastCharIndex + 1);
System.out.println("第" + number++ + "个字符串为:" + subStr
+ ";起始位置为:" + firstCharIndex + ",终止位置为:"
+ lastCharIndex);
lastCharIndex = str.lastIndexOf(lastChar, lastCharIndex - 1);
}
lastCharIndex = str.lastIndexOf(lastChar);
firstCharIndex = str.indexOf(firstChar, firstCharIndex + 1);
}
}
题2:经典面试题,将数字转化为人民币大写
/** * 转化 * @param oInt * @return */ public static String strToChinese(int oInt){ String[] s3 = {"","万","亿","兆"}; int i = 0; String s = ""; while(oInt>0){ int fourBit = oInt%10000; String temp = handleFourNumberInt(fourBit); if(s==""||(s.length()>1&&s.charAt(1)=='千')){ s = temp+s3[i]+s; }else{ s = temp+s3[i]+"零"+s; } oInt = oInt/10000; i++; } System.out.println("S:"+s); return s; } /** * 处理四位以下下整数 * @param oInt * @return */ public static String handleFourNumberInt(int oInt){ String[] s1 = {"零","一","二","三","四","五","六","七","八","九"}; String[] s2 = {"","十","百","千"}; String resultStr = ""; int i=0; while(oInt>9){ int l = oInt%10; if(l==0){//如果resultStr为空,不添加 int size = resultStr.length(); if(size==0){ ; }else if(resultStr.charAt(0)=='零'){ //如果前面已有零,不添加 ; }else{ resultStr = "零"+resultStr; } }else{ resultStr = s1[l]+s2[i]+resultStr; } oInt = oInt/10; i++; } resultStr = s1[oInt]+s2[i]+resultStr; System.out.println(resultStr); return resultStr; }
浙公网安备 33010602011771号