public class Application {
public static void main(String[] args) {
String t1 = "hello";
String t2 = new String("hello");
System.out.println("值比较" + t1.equals(t2));
String t3 = "hello word hello java";
System.out.println(t3.startsWith("hello"));
System.out.println(t3.endsWith("java"));
String t4 = " sdf sdf sdss ss ";
System.out.println(t4.trim());
System.out.println(t4.replace(" ", "1"));
System.out.println(t4.replace(" ", ""));
int index1 = t3.indexOf("hello");
int index2 = t3.indexOf("Java");
int index3 = t3.indexOf("hadoop");
int index4 = t3.lastIndexOf("hello");
System.out.println(index1 + " " + index2 + " " + index3 + " " + index4);
String t3SubString1 = t3.substring(5);
String t3SubString2 = t3.substring(6, 10);
String t3SubString3 = t3.substring(6, 16);
System.out.println(t3SubString1);
System.out.println(t3SubString2);
System.out.println(t3SubString3);
char[] t3Chars = t3.toCharArray();
for (char c : t3Chars) {
System.out.print(c + " ");
}
char[] t5Chars = new char[] { 'z', 'h', 'i', 'y', 'o', 'u' };
String t5 = new String(t5Chars);
System.out.println("\n" + t5);
String[] t3Arrays = t3.split("hello");
for (String s : t3Arrays) {
System.out.println(s);
}
String t6 = t3 + t4 + t5;
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(t3);
stringBuffer.append(t4);
stringBuffer.append(t5);
System.out.println(stringBuffer);
String t7 = stringBuffer.toString();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(t3);
stringBuilder.append(t4);
stringBuilder.append(t5);
System.out.println(stringBuilder);
String t8 = stringBuilder.toString();
System.out.println(isSymmetrical("上海自来水来自海上"));
System.out.println(isSymmetrical("上海自来水来自于海上"));
String t10 = "ABBABABAB";
String t11 = "BA";
System.out.println(timeOf(t10,t11));
}
/**
* 判断字符串是否对称
* @param str 需要判断的字符串
* @return 对称返回 true ,否则返回false
*/
public static boolean isSymmetrical(String str) {
boolean flag = true;
char[] strChars = str.toCharArray();
for(int i = 0;i < strChars.length / 2;i ++){
char a = strChars[i];
char b = strChars[strChars.length - 1 - i];
if(a != b) {
flag = false;
break;
}
}
return flag;
}
/**
* target 在 source 中出现了多少次
* @param source ABBABABAB
* @param target BA
* @return 出现的次数,如果没有返回 0
*/
public static int timeOf(String source,String target) {
int time = 0;
int index = -1;
while((index = source.indexOf(target)) != -1) {
time++;
source = source.substring(index + target.length());
}
return time;
}
}