Java String字符串详解

一、字符串String详解

1、实例化

实例化String对象:直接赋值、使用关键字new来进行开辟空间

代码
public class Test41 {
    public static void main(String[] args) {
        String str = "Hello";  //直接赋值
        System.out.println(str);
        //使用new关键字
        String str1 = new String("Hello1");
        System.out.println(str1);
    }
}
结果:
Hello
Hello1
String使用new关键字.jpg

由上图可以看出,使用new关键字实例化对象,在内存中开辟了两个空间用来存储他们,其中一个是无用的,所以使用第一种直接赋值方式更合理一些,可以更省略一些空间。

2、String的内容比较

由以下代码进行说明

代码
public class Test42 {
    public static void main(String[] args) {
        int a = 10;
        int b = 10;
        System.out.println(a==b);
        
        String str = "Hello";
        String str1 = new String("Hello"); //开辟了两个空间地址
        System.out.println(str==str1); //"==" 比较的是地址
        System.out.println(str.equals(str1)); //"equals"比较的是内容
    }
}
结果:
true
false
true

3、字符串内容不可更改

字符串.png
代码
public class Test43 {
    public static void main(String[] args) {
        String str = "hello";
         String str1 = str + " world!";
         System.out.println(str1);
    }
}
结果:
hello world!

二、String字符串常用方法

1、字符串长度

length()方法

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println("str字符串的长度:"+str.length());
    }
}
结果:
str字符串的长度:10

2、字符串转换数组

toCharArray()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        // 字符串转换成数组
        char data[] = str.toCharArray();
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
    }
}
结果:
h e l l o w o r l d 

3、从字符串中取出指定位置的字符

charAt()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        // 从字符串中取出指定位置的字符
        System.out.println(str.charAt(7));
    }
}
结果:
r

4、字符串与byte数组的转换

getBytes()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        //字符串与byte数组的转换
        byte bytes[] = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(new String(bytes)+"\t");
        }
    }
}
结果:
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  
helloworld  

5、过滤字符串中存在的字符

indexOf()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld@163.com";
        System.out.println("@字符所在的位置:"+str.indexOf("@"));
    }
}
结果:
@字符所在的位置:10

6、去掉字符串的前后空格

trim()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "  hello world";
        System.out.println(str);
        System.out.println(str.trim());
    }
}
结果:
  hello world
hello world

7、从字符串中取出子字符串

subString()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println(str.substring(4));
        System.out.println( str.substring(5));
    }
}
结果:
oworld
world

8、大小写转换

toLowerCase()toUpperCase()

代码
public class Test44 {
    public static void main(String[] args) {
        String str = "hello world";
        String str1 = "SEC";
        System.out.println(str.toUpperCase());
        System.out.println(str1.toLowerCase());
    }
}
结果:
HELLO WORLD
sec

9、判断字符串的开头结尾字符

endsWith()startWith()

代码
public class Test44 {
    public static void main(String[] args) {
        String email = "sec_hello@126.com";
        System.out.println(email.startsWith("sec"));
        System.out.println(email.endsWith("com"));
        System.out.println(email.endsWith("163.com"));
    }
}
结果:
true
true
false

10、替换String字符串中的一个字符

replace()

代码
public class Test44 {
    public static void main(String[] args) {
        String email = "sec_hello@126.com";
        System.out.println("将字符串中的e替换成m:"+email.replace('e', 'm'));
    }
}
结果:
将字符串中的e替换成m:smc_hmllo@126.com

三、StringBuffer方法

1、认识StringBuffer

缓冲区,本身也是操作字符串,但是与String不同,StringBuffer是可以更改的。
StringBuffer是一个操作类,所以必须通过实例化进行操作。

代码
public class Test45 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello");
        System.out.println("StringBuffer更改前:"+sBuffer.toString());
        tell(sBuffer);
        System.out.println("StringBuffer更改后:"+sBuffer.toString());
        
        String string = "android";
        System.out.println("String更改前:"+string);
        tell1(string);
        System.out.println("String更改后:"+string);
    }
    
    public static void tell(StringBuffer s) {
        s.append(" I love sec");
    }
    
    public static void tell1(String str) {
        str = "java";
    }
}
结果:
StringBuffer更改前:hello
StringBuffer更改后:hello I love sec
String更改前:android
String更改后:android

2、StringBuffer常用方法

append() 追加

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello ");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
    }
}
结果:
hello world

insert() 插入

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello ");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
        
        sBuffer.insert(6, "love "); //从第7个位置开始插入字符love 
        System.out.println(sBuffer.toString());
    }
}
结果:
hello world
hello love world

replace() 替换

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello ");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
        sBuffer.replace(1, 4, "wwtcom");//从第2个位置到第4个位置替换成"wwtcom"
        System.out.println(sBuffer.toString());
    }
}
结果:
hello world
hwwtcomo world

indexOf() 字符串存在的位置

示例
public class Test46 {
    public static void main(String[] args) {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello@");
        sBuffer.append("world");
        System.out.println(sBuffer.toString());
        System.out.println("@字符存在的位置:"+sBuffer.indexOf("@"));
    }
}
结果:
hello@world
@字符存在的位置:5

3、StringBuffer类的应用

代码
public class Test47 {
    public static void main(String[] args) {
        String str = "hello";
        for (int i = 0; i < 100; i++) {
            str = str + i;
        }
        System.out.println(str); //使用String需要开辟101个空间,很耗资源
        
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("hello");
        for (int i = 0; i < 100; i++) {
            sBuffer.append(i);
        }
        System.out.println(sBuffer.toString());//使用StringBuffer运行很快,不需要重新开辟空间
    }
}
结果:
hello0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
hello0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899

四、StringBuider用法

  • 一个可变的字符序列,该类被设计作用StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候。建议优先考虑该类,速度比StringBuffer要快。
  • 但是如果设计到线程安全,建议使用StringBuffer。
  • 常用方法:append()insert() 使用方式与StringBuffer是一样的。
代码
public class Test48 {
    public static void main(String[] args) {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append("Hello ");
        sBuilder.append("world");
        System.out.println("sBuilder更改前:"+sBuilder.toString());
        
        sBuilder.insert(6, "love "); //从第7个位置开始插入字符love 
        System.out.println("sBuilder更改后:"+sBuilder.toString());
    }
}
结果:
sBuilder更改前:Hello world
sBuilder更改后:Hello love world
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • java中String的常用方法 1、length()字符串的长度 例:char chars[]={'a','b'...
    赤赤有名阅读 2,023评论 0 10
  • 前面我们总结了数组操作,这里我们将总结字符串相关的知识,除了总结String的API用法,同时我们还会总结一些相关...
    HCherisher阅读 3,585评论 2 6
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,531评论 18 399
  • 一、String 类 1、定义: 1、从概念上讲,java字符串就是Unicode字符序列。每个用双引号括起来的字...
    玉圣阅读 1,554评论 0 1
  • 构造方法的重载 构造方法的重载是指在同一个类中存在着若干个具有不同参数列表的构造方法。有时在一个类定义内可能出现多...
    陈老板_阅读 303评论 0 0