String字符串转多种类型及多种方法的应用

  • Post author:
  • Post category:其他


public class StringDemo3 {


public static void main(String[] args) {


fun9();

}

//9  boolean  equals(Object obj);  判断字符串里面完全相等 返回true

//s.equalsIgnoreCase(s1)  不区分大小写的比较

public static void fun9(){


String s = “hello”;

String s1= “hEllo”;

System.out.println(s.equals(s1));

System.out.println(s.equalsIgnoreCase(s1));

}

//8 将字符串  转字符数组

public static void fun8(){


String s = “今晚11点老地方见 不见不散”;

char[] s1 = s.toCharArray();

for (int i = 0; i < s1.length; i++) {


System.out.println(s1[i]);

}

}

//7 将字符串转字节数组  getBytes();

public static void fun7(){


String s = “今晚11点老地方见 不见不散”;

byte[] s1 = s.getBytes();

//System.out.println(s1);

for (int i = 0; i < s1.length; i++) {


System.out.println(s1[i]);

}

}

// 6 查找一个字符  indexOf(char ch)  返回int   返回-1 没有找到

public static void fun6(){


String s = “hello.java”;

int s1 = s.indexOf(‘w’);

System.out.println(s1);

}

//5  contains判断一个字符串是否有另一个字符串

public static void fun5(){


String s = “hello.java”;

boolean s1 = s.contains(“ll”);

System.out.println(s1);

}

//4判断一个字符串的后缀 结尾   endsWith(“参数”);

public static void fun4(){


String s = “hello.java”;

boolean s1 = s.endsWith(“.java”);

System.out.println(s1);

}

//3boolean statsWith(String preFix)  判断一个字符串是否包含另一个字符串

public static void fun3(){


String s = “helloworld”;

boolean s1 = s.startsWith(“hello”);

System.out.println(s1);

}

//2 substring(int beginIndex,int endIndex)获取字符串的一部分 包含头 不包含尾

//substring(int beginIndex)  后面的全要

public static void fun2(){


String s = “helloworld”;

String s2 = s.substring(2);

//String s1 = s.substring(1, 4);

System.out.println(s2);

}

// 1 int length();  返回的是字符串的长度

public static void fun1(){


String string = “skjl”;

int l = string.length();

System.out.println(l);

}

}

转载于:https://www.cnblogs.com/lxy4/p/10554574.html