玖叶教程网

前端编程开发入门

Java字符串常用方法(java字符串常用方法详解)

package model3.unit1;


public class Java04 {

public static void main(String[] args) {


// (4)字符串常用方法


// 长度

int a = "abcde".length();

System.out.println(a);

// 截取

String b = "abcde".substring(2);

System.out.println(b);

// 截取

String c = "abcde".substring(1, 4);

System.out.println(c);

// 去除空格

String d = " abc ".trim();

System.out.println(d);


}

}


package model3.unit1;


public class Java05 {

public static void main(String[] args) {


// 字符串常用方法


// 大写

String a = "abcDE".toUpperCase();

System.out.println(a);

// 小写

String b = "BbC".toLowerCase();

System.out.println(b);

// 不区分大小写相等

boolean c = "aBc".equals("Abc");

boolean d = "aBc".equalsIgnoreCase("Abc");

System.out.println(c);

System.out.println(d);

// 判断开头

boolean e = "abcd".startsWith("ab");

System.out.println(e);

// 判断结尾

boolean f = "abcde".endsWith("de");

System.out.println(f);


}

}


package model3.unit1;


public class Java06 {

public static void main(String[] args) {

// 字符串常用方法


// 是否包含

boolean a = "abc".contains("bc");

System.out.println(a);

// 替换

String b = "abcabcabc".replaceAll("b", "张三");

System.out.println(b);

// 判断空字符串

boolean c = "".isEmpty();

System.out.println(c);

// 字符串转换char[]

char[] d = "abcde".toCharArray();

for (char x:d) {

System.out.println(x);

}

// 分割字符串

String[] arr = "张三,李四,王五".split(",");

for (String n:arr) {

System.out.println(n);

}


}

}

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言