java字符串

来源:互联网 发布:淘宝怎样用花呗付款 编辑:程序博客网 时间:2024/06/07 06:44

生活中的字符串

  • 1.提示信息;
  • 2.hello World
  • 3.”教育改变生活”
String  = "hello World";//最简单String s = new String();//空字符串String s = new String("hello World")//标准语法

字符串长度

  • String类提供了length()方法,确定字符串的长度,返回值为int类型;
    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("请输入用户名:");        String name = input.next();        System.out.println("请输入密码:");        String psw = input.next();        if (psw.length()<6){            System.out.print("密码长度不能小于6位!");        }else{            System.out.print("注册成功!");    }

字符串比较

  • String类提供了equals()方法,比较两个字符串是否相等,返回值为布尔类型;
    区分大小写,检查组成字符串内容 的字符是否完全一致;
if (psw.length()<6){            System.out.print("密码长度不能小于6位!");        }else if(name.equals("tom")&&psw.equals("123456"))            System.out.print("注册成功!");        }else{            System.out.print("账号或密码错误");            }
  • “==”用于比较字符串,比较的是内存空间首地址是否相等,即判断是否是同一个字符串,返回值也是布尔类型.

字符串常量池

  • 当创建一个常量池中已有的字符串时,为了节省空间,它并不会创建一个新的字符串;而是将新创建的字符串地址指向已有的字符串;如果没有,才会在字符串常量池中开辟一个新的空间.

  • 而基本数据类型的存储区域在栈中,每一个数据都是独立存在的.”==”比较的是两个数据的大小.

  • 数组是引用数据类型,数据占用两块空间,栈和堆,栈中存放数组的数据在堆中存在的首地址(数组名);堆里面存放的是数组数据

public static void main(String[] args) {    //基本数据类型          int i = 5;        int j = 5;        System.out.println(i==j);//true        //引用数据类型 数组        int arr1[] = new int[2];        int arr2[] = new int[2];        arr1[0] = 1;        arr1[1] = 2;        arr2[0] = 1;        arr2[1] = 2;        System.out.println(arr1==arr2); //false        System.out.println(Arrays.equals(arr1, arr2));//true        //字符串        String s = "qh";        String s2 = "qh";        System.out.println(s == s2);//true    //在堆里面创建 一个引用 两个实例  引用在栈里面,一个实例在堆里面,另一个实例在     //字符串常量池里面.(new 关键字 在堆里面创建)        String s3 = new String("jereh");;        String s4 = new String("jereh");//在堆里面创建        System.out.println(s3 == s4);//false    }  

数据在内存中的存放情况如图所示

字符串连接

  • 方法1:”+”;
  • 方法2:concat()方法
public static void main(String[] args) {        String string = new String("你好,");        String name = new String("张三!");        String sentence= string.concat(name);        System.out.print(sentence);    }

字符串提取方法

  • public int indexOf( )如果没有找到返回-1;
  • substring(int intdex) 从位置索引处开始截取到字符串结束
public static void main(String[] args) {        String s = "jredu.java";        String subs = s.substring(5);        System.out.println(subs);//返回值为.java    }
  • substring(intdex1(包含),index2(不包含)) 从位置index1处开始截取到字符串到index2结束

  • indexOf()获取字符串中某个字符或字符串首次出现的位置;

System.out.println(s.indexOf("e"));  //返回2
  • lastIndexOf()获取字符串中某个字符或字符串最后一次出现的位置;
  • trim() 去掉字符串前后的空格;
        String s2 = "  我爱我家  ";        String s3 = s2.trim();//返回一个去掉前后空格的新字符串;        System.out.println(s3);
  • split()方法,讲一个字符串分割为子字符串,然后将子字符串放在一个字符串数组中返回;
public static void main(String[] args) {        String s = "长亭外 古道边 芳草碧连天 晚风拂柳笛声残 夕阳山外山";        System.out.println(s);        String[] S2 =s.split(" ");//遇空格隔开        System.out.println(Arrays.toString(S2)); //打印数组;        //返回[长亭外, 古道边, 芳草碧连天, 晚风拂柳笛声残, 夕阳山外山]    }

其他方法

  • equalsIgnoreCase()忽略大小写判断是否相等
  • tolowcase ()转换为小写
  • toupcase()转换为大写

StringBuffer 类

String 的增强类;操作字符串更简单便捷;

StringBuffer sBuffer  = new StringBuffer("青春无悔");//创建StringBuffer类型的变量 ;
  • toString() 返回此序列中数据的字符串表示形式。
  • apend()方法;
    public static void main(String[] args) {        StringBuffer sBuffer  = new StringBuffer("青春无悔");        StringBuffer sBuffer2 = sBuffer.append("我心永恒");//青春无悔我心永恒        System.out.println(sBuffer);        StringBuffer sBuffer3 = sBuffer2.append("啊");        System.out.println(sBuffer3); //青春无悔我心永恒啊
  • insert(index,content)方法;
Scanner input =new Scanner(System.in);        System.out.println("请输入要处理的数据:");        String s2 = input.next();        StringBuffer s =  new StringBuffer(s2);        for(int i= s.length()-3;i>0;i-=3){            s.insert(i,",");//从后往前每三位插入","        }        System.out.println(s.toString());//转换为字符串输出;        input.close();//及时关闭,消耗内存太大
  • indexOf(String str) 返回第一次出现的指定子字符串在该字符串中的索引。
  • deleteChatAt() 删除指定位置的字符串;
  • delete(start,end)从开始位置删除到结束位置;
  • buffer.reverse 方法 倒叙
  • buffer.replace(开始位置,结束为止,替换内容)
原创粉丝点击