Java------常用类之String类

String类

String

  • 字符串是常量,创建之后不可修改
  • 字符串字面值存储在字符串池中,可以共享

在JDK6.0及之前版本,字符串常量池是放在Perm Gen区(也就是方法区);但在JDK7.0版本,字符串常量池被移到了堆中了

  • String s = “Hello”:产生一个对象,字符串池中存储
  • String s = new String (“hello”):产生两个对象,堆,池各存储一个
public class Demo03 {
    public static void main(String[] args) {
        String name = "hello";//"hello"存储在字符串池
        //"wanji"赋值给name变量,给字符串赋值是,并没有修改数据,而是重新开辟一个空间
        name = "wanji";
        //当name2创建后,会先在字符串池中搜索,如果没有就重新开辟空间,如果有则指向相同,地址一样
        String name2 = "wanji";

//        演示字符串的另一种创建方式
        String str = new String("采三秀兮于山间,石磊磊兮葛蔓蔓");
        String str2 = new String("采三秀兮于山间,石磊磊兮葛蔓蔓");
        System.out.println(str);
        System.out.println(str2);
        System.out.println(str==str2);
        System.out.println(str.equals(str2));
    }
}

运行结果:

采三秀兮于山间,石磊磊兮葛蔓蔓
采三秀兮于山间,石磊磊兮葛蔓蔓
false
true

==比较的是内存地址,.equals比较的是值

常用方法

  • public int length () : 返回字符串长度
  • public char charAt(int index) : 根据下标获取字符
  • public boolean contains(String str) : 判断当前字符串是否包含str
       String title = "采三秀兮于山间,石磊磊兮葛蔓蔓";
/*        字符串方法的使用:
           1.length();返回字符串长度
           2.charAt(int index);返回某个位置的字符
           3.contains(String str);判断是否彪悍某个字符串
 */

		//1开始;空格也算一个字符
        System.out.println(title.length());
		
		//0开始
        System.out.println(title.charAt(title.length()-1));

        System.out.println(title.contains("蔓"));
        System.out.println(title.contains("涛"));

运行结果:

15true
false
  • public char[] toCharArray : 将字符串转换成数组
  • public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标,不存在则返回-1
  • public int lastIndexOf(String str) : 查找字符串在当前字符串中最后一次出现的下标索引
/*
        4.toCharArray();返回字符串对应的数组
        5.indexOf();返回子字符串首次出现的位置
        6.lastIndexOf();返回字符串最后一次出现的位置
 */
//        System.out.println(title.toCharArray());打印的是它的位置
        System.out.println(Arrays.toString(title.toCharArray()));

        System.out.println(title.indexOf("兮"));
//        从第5个位置开始找"兮"
        System.out.println(title.indexOf("兮",5));

        System.out.println(title.lastIndexOf("蔓"));

运行结果:

[,,,,,,, ,,,,,,,,]
3
11
14
  • public String trim() : 去掉字符串前后的空格
  • public String toUpperCase() : 将小写转成大写
  • public boolean endWith(String str) : 判断字符串是否以str结尾
/*
        7.trim();去掉字符串前后的空格
        8.toUpperCase();把小写转换成大写
        	toLowerCase(); 把大写转换成小写
        9.endWith(str) ; 判断是否str结尾  
        	startWith(str);判断是否以str开头
 */
        String title2 = "    Gather three beauties in the mountains, Shi Lei Lei and Ge man   " ;
        System.out.println(title2.trim());

        System.out.println(title2.toUpperCase());
        System.out.println(title2.toLowerCase());

        String title3 = "Gather three beauties in the mountains, Shi Lei Lei and Ge man";
        System.out.println(title3.endsWith("man"));
        System.out.println(title3.startsWith("G"));

运行结果:

Gather three beauties in the mountains, Shi Lei Lei and Ge man
    GATHER THREE BEAUTIES IN THE MOUNTAINS, SHI LEI LEI AND GE MAN   
    gather three beauties in the mountains, shi lei lei and ge man   
true
true
  • public String replace (char oldChar , char newChar): 将旧字符串替换成新字符串
  • public String[] split(str) : 根据str做拆分
  /*
 10.replace(char old , char new );用新字符或字符串替换旧的字符或字符串
 11.split();对字符进行拆分
 */

        System.out.println(title.replace("蔓蔓","慢慢"));

//        []表示选项:选择空格和逗号  +表示出现一个或多个
        String[] arr = title3.split("[ ,]+");
        System.out.println(title3.length());
        for (String s : arr) {
            System.out.println(s);
        }

运行结果:

采三秀兮于山间,石磊磊兮葛慢慢
62
Gather
three
beauties
in
the
mountains
Shi
Lei
Lei
and
Ge
man

补充:

补充两个方法equlas \ compare();比较大小

        System.out.println("------补充------");

        String s1 = "hello";
        String s2 = "HELLO";
//        equalsIgnoreCase忽略大小写的比较
        System.out.println(s1.equalsIgnoreCase(s2));

        String s3 = "abc" ; //97
        String s4 = "xyz";//120
        System.out.println(s3.compareTo(s4));//s3 -s4 的位置

        String s5 = "abc";
        String s6 = "abcxyz";
        System.out.println(s5.compareTo(s6));//s5的长度 - s6 的长度

运行结果:

------补充------
true
-23
-3

String案例演示

  • 需求:
    • 已知String str = “this is a text” ;
    • 将str中的单词单独获取出来
    • 将str中的text替换为practice
    • 在text前面插入一个easy
    • 将每个单词的首字母改为大写
package com.Innerclass.lesson05;

public class Demo05 {
    public static void main(String[] args) {
        String str = "this is a text";

//        1.将str中的单词单独获取出来
        String[] arr = str.split(" ");
        for (String s : arr) {
            System.out.println(s);
        }

//        2.将str中的text替换为practice
       String str2 =  str.replace("text","practice");
        System.out.println(str2);

//        3.在text前面插入一个easy
        String str3 = str.replace("text","easy text");
        System.out.println(str3);

//        4.将每个单词的首字母改为大写
        for (int i = 0; i < arr.length; i++) {
            char first = arr[i].charAt(0);
//            把第一个字符转换成大写
            char upperfirst = Character.toUpperCase(first);
            String news = upperfirst+arr[i].substring(1);
            System.out.print(news+" ");
        }
    }
}

运行结果:

this
is
a
text
this is a practice
this is a easy text
This Is A Text 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值