zoukankan      html  css  js  c++  java
  • Java字符串String详解

    -----------siwuxie095

       

       

       

    1、String字符串

       

    实例化String对象:

    1)直接赋值,如:String str="hello";

    2)使用关键字 new,如:String str=new String("hello");

       

       

       

    由图可知:使用 new 的方式在堆内存中开辟了两个空间,

    第一个 "hello",对象 str 没有指向,无用等待回收,

    第二个 "hello",被 str 指向,有用。

       

    所以 直接赋值(只开辟了一个空间) 的方式更常用和合理,

    可以节省一些空间

       

       

       

    String的内容比较

       

    代码:

       

    package com.siwuxie095.string;

       

    public class StringDemo01 {

       

    public static void main(String[] args) {

    String str="hello";

    String str1=new String("hello");

    //str str1 比较结果

    //双等比较的是地址 结果为false

    System.out.println(str==str1);

    //equals()比较的是内容 结果为true

    System.out.println(str.equals(str1));

    }

       

    }

       

       

    运行一览:

    显然,双等是错误的,而使用方法 equals() 是正确的

       

    〔因为双等比较的是内存地址,第一种方式直接赋值只产生了一个地址,

    第二种 new 的方式却产生了两个地址,注定地址是不同的,而 equals()

    比较的是内容〕

       

       

       

       

    字符串的内容不可更改

       

    如:

       

       

    代码:

       

    package com.siwuxie095.string;

       

    public class StringDemo02 {

       

    public static void main(String[] args) {

    String str="hello";

    str=str+" world!";

    System.out.println(str);

    }

       

    }

       

    运行一览:

    "hello"、"world"、"hello world!" 分别开辟了三个空间,

    str 指向 "hello" 的连接断开,继而指向 "hello world!",

    "hello" 本身没有变化,变的只是 str 中对堆内存地址

    的指向

       

       

       

       

       

       

    2、String字符串常用方法

       

    String字符串的方法较多,可以根据API给出的方法去做测试,下面是常用方法:

       

    1)字符串长度:length() 「数组中的是 length 属性」

       

    2)字符串转换数组:toCharArray()

       

    3)从字符串中取出指定位置的字符:charAt()

       

    4)字符串与byte数组的转换:getBytes()

       

    5)过滤字符串中存在的字符:indexOf()

       

    6)去掉字符串的前后空格:trim() 「空格易影响到对字符串的判断,需适时去掉」

       

    7)从字符串中取出子字符串:subString()

       

    8)大小写转换:toLowerCase() toUpperCase()

       

    (9)判断字符串的开头结尾字符:startWith() endsWith()

       

    (10)替换String字符串中的一个字符:replace()

       

    代码:

       

    package com.siwuxie095.array;

       

       

    public class StringDemo03 {

       

    public static void main(String[] args) {

    String str="siwuxie095";

    //(1)

    System.out.println(str.length());

    //(2)

    char data[]=str.toCharArray();

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

    System.out.print(data[i]+" ");

    }

    //(3)

    System.out.println(" "+str.charAt(0));

    //(4)

    byte bytes[]=str.getBytes();

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

    System.out.println(new String(bytes));

    }

    //(5) 若存在,返回当前字符的位置,否则返回 -1

    System.out.println(str.indexOf("x"));

     

    //(6)

    str=" siwuxie095";

    System.out.println(str.trim());

    //...

    }

       

    }

       

       

       

       

       

    3、StringBuffer

       

    StringBuffer,是String的缓冲区,本身也是操作字符串用的,

    但与String不同,StringBuffer是可以更改的

       

    〔String是不可更改的,每次看到更改内容,其实都是在开辟新的

    内存空间,非常耗资源,此时可以使用StringBuffer〕

       

    StringBuffer是一个操作类,所以必须通过实例化进行操作

       

    代码:

       

    package com.siwuxie095.string;

       

    public class StringBufferDemo01 {

       

    public static void main(String[] args) {

    StringBuffer sb=new StringBuffer();

    //通过 append()方法添加内容(追加)

    sb.append("hello");

    //使用 toString()将之转换成String类型

    //毕竟StringStringBuffer不是同一类型

    System.out.println(sb.toString());

    tell(sb);

    System.out.println(sb.toString());

    }

     

    public static void tell(StringBuffer s) {

    s.append(" world!");

    }

       

    }

     

       

    运行一览:说明StringBuffer是可以更改的

       

       

       

    对比:

       

    package com.siwuxie095.string;

       

    public class StringBufferDemo02 {

       

    public static void main(String[] args) {

    String sb="hello";

    System.out.println(sb);

    tell(sb);

    System.out.println(sb);

    }

     

    public static void tell(String s) {

    s=" world!";

    }

       

    }

       

    运行一览:说明String是不可更改的

       

       

       

       

    StringBuffer的常用方法:

    append() insert() replace() indexOf()

       

    代码:

       

    package com.siwuxie095.string;

       

    public class StringBufferDemo03 {

       

    public static void main(String[] args) {

    StringBuffer sb=new StringBuffer();

    sb.append("hello");

    sb.insert(0, "hi");

    System.out.println(sb.toString());

    sb.replace(1, 4, "www");

    System.out.println(sb.toString());

    }

       

    }

       

       

    运行一览:

       

       

       

       

    StringBuffer类的应用:

       

    为什么有的时候要用StringBuffer,而不用String:

       

    代码:

       

    package com.siwuxie095.string;

       

    public class StringBufferDemo04 {

       

    public static void main(String[] args) {

    String str="hello";

    for (int i = 0; i < 100; i++) {

    //会自动把int型的i转换为String类型

    //每加一次就会开辟一个空间来储存新的

    //最后开辟了100个,耗费资源

    str=str+i;

    }

    System.out.println(str);

     

    //使用StringBuffer不会耗费资源,且会更快

    //所以很多时候使用StringBuffer

    StringBuffer sb=new StringBuffer();

    sb.append("hello");

    for (int i = 0; i < 100; i++) {

    sb.append(i);

    }

    System.out.println(sb);

    }

       

    }

       

       

       

       

       

       

    4、StringBuilder

       

    一个可变的字符序列,该类被设计作用StringBuffer的一个简易替换,

    用在字符串缓冲区被单个线程使用的时候,建议优先考虑该类,速度

    比StringBuffer要快

       

    但如果涉及到线程安全方面,建议使用StringBuffer

       

    常用方法:

    append() insert() …

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    Exceptionless in Docker on Linux 搭建及部署疑难杂症
    Golang 基础语法介绍及对比(二)
    Go Windows 环境安装及配置(一)
    ElasticSearch搜索解析
    ElasticSearch文档及分布式文档存储
    ElasticSearch利用IK实现全文搜索
    ElasticSearch简要总览
    轻松驾驭统计学——数据分析必备技能
    【面试】数据分析常见问题
    【Echarts】
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6551600.html
Copyright © 2011-2022 走看看