Java中的字符串相关解读

 Java字符串就是Unicode字符序列。Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义类String,位于java.lang包中,编译器会默认导入这个包。字符串被作为String类型的对象来处理。

  创建String对象的方法:

  1、String s = "Hello World";

  2、String s = new String();

  3、String s = new String("Hello World");

  获取字符串的长度:字符串名。length();

  字符串比较:字符串1.equals(字符串2);//相同的字母,大小写不同也是不相同

  字符串1.equalsIgnoreCase(字符串2);//比较时忽略大小写

  toLowerCase()  转换字符串中的英文字符为小写

  toUpperCase()  转换字符串中的英文字符为大写

   字符串连接:

  1、使用"+"运算符,

  2、使用String类的concat()方法

  字符串常用的提取和查询方法:

  方法 说明

  public  int  indexOf(int  ch) 搜索第一个出现的字符ch

  public  int  indexOf(String  value)  搜索第一个出现的字符串value

  public  int  lastIndexOf(int  ch) 搜索最后一个出现的字符ch

  public  int  lastIndexOf(String Value) 搜索最后一个出现的字符串value

  public  String  substring  (int  index) 提取从位置索引开始的字符串部分

  public  String  substring(int beginIndex,  int  endIndex); 提取beginIndex和endIndex之间的字符串部分

  public  String  trim(  ) 去除字符串的前后空格,并返回副本

 

你可能感兴趣的:(java)