JavaString类的常见方法

一、String类型的创建

  1. 直接通过String创建的变量不可修改(栈内存)

如果前一个String变量被覆盖了,依然会存在,例如
String s1 = "abc"; Sring s1 = "def"; System.out.println(s1+s2);
这里相当于产生了三个String类型的变量放在栈内存(串池)中
如果在栈内存(串池)中定义重复的String类型变量,系统不会重新
  1. 直接创建String对象 new出来的对象可随意修改(堆内存)

创建方法 : String s = new String(字符串参数);
可以使用多种传入参数构建字符串对象
String s1 = new String("abc");//直接创建字符串 String s2 = new String(char[]);//传入字符数组 String s2 = new String(char[],start,end); //从start开始到end创建字符串对象 String s3 = new String(byte[]);//传入字节数组 String s3 = new String(byte[],start,end); //从start开始到end创建字符串对象


二、String类型的的存储结构

  • $\color{Goldenrod}{直接创建的String变量是创建在串池中的,串池位于栈内存中,栈内存中不允许重复数据}$

  • $\color{Pink}{通过创建的String对象是位于堆内存中的,允许重复,每一个创建的String对象都会新增一个对应的地址,每new一个对象都会出现一个新的地址空间}$

  • $\color{Orange}{一般使用直接创建的String变量,String对象会浪费内存空间}$

三、String类型的比较

  • 直接通过String创建的变量比较可以用 “==” 来比较是否相等

String str1 、 str2;
str1 == str2;
  • String类型的创建对象不能用“==” 比较相等 , 必须调用String类里重写的 equals 方法

str1 = new("str");
str2 = new("str");
str1.equals(str2);


四、String常用方法

1. char charAt( index );

返回String索引处的 char类型

char c = str.charAt(1);

2. int compareTo( str );

根据字典顺序比较两个字符串的大小 只有 -1 0 1 三种返回值

int result = "abcde".compareTo("abcda");
//result =1

返回值

含义

-1

前者小于后者

0

两两相等

1

前者大于后者

3. boolean contains( str );

判断是否包含字符串 str

boolean result = "hello world.java".contains(hello world);  
//result = true

4. boolean endsWith( str );

>判断是否以字符串 str 结尾

startsWith( str )
判断字符串是否以 str 开始
boolean result = "hello world".endsWith("world"); 
//result = true

boolean result = "hello world".startsWith("hello");
//result = true;

5. boolean equals( str );

>判断字符串是否相等

equalsIgnoreCase()
无视大小的写判断字符串是否相等
boolean result = "hello".equals"hello"; 
//result = true

boolean result = "hello".equalsIgnoreCase"hello"; 
//result = true

6. void getChars( begin , end , char[] , 偏移量 );

> 复制字符串的 start - end 内容到 给定的数组内

- void getByte ( begin , end , byte[] , 偏移量 ); 同理
String str = new String("hello world");

byte [] bytes; 

str.getBytes( 0, 4, bytes, 0);
//bytes[] =  h e l l o

7. int indexOf( str );

> 返回 str 在字符串中第一次出现的索引

String str = new String("hello world");
int index = str.indexOf("el");
//index = 1

8. int lastIndexOf( str );

> 返回 str 在字符串中最后一次出现的索引

String str = new String("hello world");
int index = str.lastIndexOf("l");
//index = 9

9. boolean isEmpty( );

>判断字符串是否为空

10. int length( );

> 返回字符串长度

warning

  • 字符串的 length 是方法

  • 数组的length 是数组自带的属性

11. String replace( oldStr , newStr );

> 将 newStr 替换 oldStr 并返回一个新的String对象

String str = "hello world".replace("hello","hi");
// str = "hi world"
  • replaceAll( oldstr , replacement);

使用正则表达式 replacement 替换所有的 oldstr
  • replaceFirst( oldstr , replacement);

12. String split( str );

> 以 str 作为分割线切割字符串

str 可以是 字符串 也可以是 正则表达式
String str = new String("h-e-l-l-o");
str.split("-");
System.out.println(str);
// str = "hello"

13. String substring( start , end );

> 从start开始到end结束 左闭右开: [start , end) 切割字符串

不带end 自动切割到最后一个字符
String str = new String("hello world");
str.substring(0,6);
//str = "world"

str.substring(5)
//str = "hello"

14. char[] toCharArray( );

> 将字符串转换为 字符数组

String str = new String("hello world");
char[] chars = str.toCharArray();

15. String toLowerCase( );

> 所有字符转换成小写

String toUpperCase( );
所有字符转换成大写

16. String trim( );

> 去除字符串的前后空白

17.static String valueOf( Object obj );

> 将非字String类型转换为String类型

    • Valueof 方法是静态的 直接用 String.valueOf( obj ) 调用

  • valueOf 方法在执行时 会调用传入参数对象的 toSting 方法 , 这时需要 重写参数对象的 toString 方法

class Person{
  public String toString(){
    return "我变成Sting类型对象了";
  }
}

Person p = new Person();
String str = String.valueOf(p);
System.out.println( str ); //自动调用str的toString 方法

1. 直接通过String创建的变量不可修改(栈内存)

>如果前一个String变量被覆盖了,依然会存在,例如

>```java

>String s1 = "abc";

>Sring s1 = "def";

>System.out.println(s1+s2);

>```

>这里相当于产生了三个String类型的变量放在栈内存(串池)中

>如果在栈内存(串池)中定义重复的String类型变量,系统不会重新

2. 直接创建String对象 new出来的对象可随意修改(堆内存)

>创建方法 :*`String s = new String(字符串参数);`*

>可以使用多种传入参数构建字符串对象

>```java

>String s1 = new String("abc");//直接创建字符串

>String s2 = new String(char[]);//传入字符数组

>String s2 = new String(char[],start,end); //从start开始到end创建字符串对象

>String s3 = new String(byte[]);//传入字节数组

>String s3 = new String(byte[],start,end); //从start开始到end创建字符串对象

>```

<br>

-------

# 二、String类型的的存储结构

- $\color{Goldenrod}{直接创建的String变量是创建在串池中的,串池位于栈内存中,栈内存中不允许重复数据}$

- $\color{Pink}{通过创建的String对象是位于堆内存中的,允许重复,每一个创建的String对象都会新增一个对应的地址,每new一个对象都会出现一个新的地址空间}$

- $\color{Orange}{一般使用直接创建的String变量,String对象会浪费内存空间}$

<br>

<br>

# 三、String类型的比较

- 直接通过String创建的变量比较可以用 “==” 来比较是否相等

```java

String str1 、 str2;

str1 == str2;

```

- String类型的创建对象不能用“==” 比较相等 , **<font color = #FF6EC7>必须调用String类里重写的 equals 方法</font>**<br>

```java

str1 = new("str");

str2 = new("str");

str1.equals(str2);

```

<br>

------

# 四、String常用方法

<br>

## ***<font color = #FF6EC7>1. char charAt( index );</font>***

***<font color = #00FFFF>返回String索引处的 char类型</font>***<br>

```java

char c = str.charAt(1);

```

<br>

<br>

## ***<font color = #FF6EC7>2. int compareTo( str );</font>***

***<font color = #00FFFF>根据字典顺序比较两个字符串的大小 只有 -1 0 1 三种返回值</font>***<br>

```java

int result = "abcde".compareTo("abcda");

//result =1

```

<br>

|返回值|含义|

|:---:|:---:|

|-1|前者小于后者|

|0|两两相等|

|1|前者大于后者|

<br>

## ***<font color = #FF6EC7>3. boolean contains( str );</font>***

***<font color = #00FFFF>判断是否包含字符串 str</font>***

```java

boolean result = "hello world.java".contains(hello world);

//result = true

```

<br>

<br>

## ***<font color = #FF6EC7>4. boolean endsWith( str );</font>***

***<font color = #00FFFF>>判断是否以字符串 str 结尾</font>***<br>

>- ### ***<font color = #FF6EC7>startsWith( str )</font>***<br>

>

>***判断字符串是否以 str 开始***

```java

boolean result = "hello world".endsWith("world");

//result = true

boolean result = "hello world".startsWith("hello");

//result = true;

```

<br>

<br>

## ***<font color = #FF6EC7>5. boolean equals( str );</font>***

<br>

***<font color = #00FFFF>>判断字符串是否相等 </font>***<br>

>- ### ***<font color = #FF6EC7>equalsIgnoreCase()</font>***<br>

>

>***无视大小的写判断字符串是否相等***

```java

boolean result = "hello".equals"hello";

//result = true

boolean result = "hello".equalsIgnoreCase"hello";

//result = true

```

<br>

## ***<font color = #FF6EC7>6. void getChars( begin , end , char[] , 偏移量 );</font>***

***<font color = #00FFFF>> 复制字符串的 start - end 内容到 给定的数组内</font>***

>-***<font color = #FF6EC7> void getByte ( begin , end , byte[] , 偏移量 ); 同理</font>***

```java

String str = new String("hello world");

byte [] bytes;

str.getBytes( 0, 4, bytes, 0);

//bytes[] = h e l l o

```

<br>

## ***<font color = #FF6EC7>7. int indexOf( str );</font>***

***<font color = #00FFFF>> 返回 str 在字符串中第一次出现的索引 </font>***

```java

String str = new String("hello world");

int index = str.indexOf("el");

//index = 1

```

<br>

## ***<font color = #FF6EC7>8. int lastIndexOf( str );</font>***

***<font color = #00FFFF>> 返回 str 在字符串中最后一次出现的索引 </font>***

```java

String str = new String("hello world");

int index = str.lastIndexOf("l");

//index = 9

```

<br>

## ***<font color = #FF6EC7>9. boolean isEmpty( );</font>***

***<font color = #00FFFF>>判断字符串是否为空 </font>***

<br>

## ***<font color = #FF6EC7>10. int length( );</font>***

***<font color = #00FFFF>> 返回字符串长度 </font>***<br>

## **<font style = "background:#BC1717">warning</font>**

- 字符串的 length 是方法

- 数组的length 是数组自带的属性

<br>

## ***<font color = #FF6EC7>11. String replace( oldStr , newStr );</font>***

***<font color = #00FFFF>> 将 newStr 替换 oldStr 并返回一个新的String对象</font>***

```java

String str = "hello world".replace("hello","hi");

// str = "hi world"

```

- ***replaceAll( oldstr , replacement);***

>使用正则表达式 replacement 替换所有的 oldstr

- ***replaceFirst( oldstr , replacement);***

<br>

>使用正则表达式 replacement 替换第一个 oldstr

<br>

## ***<font color = #FF6EC7>12. String split( str );</font>***

***<font color = #00FFFF>> 以 str 作为分割线切割字符串 </font>***

>str 可以是 字符串 也可以是 正则表达式

```java

String str = new String("h-e-l-l-o");

str.split("-");

System.out.println(str);

// str = "hello"

```

<br>

## ***<font color = #FF6EC7>13. String substring( start , end );</font>***

***<font color = #00FFFF>> 从start开始到end结束 左闭右开: [start , end) 切割字符串 </font>***

> 不带end 自动切割到最后一个字符

```java

String str = new String("hello world");

str.substring(0,6);

//str = "world"

str.substring(5)

//str = "hello"

```

<br>

## ***<font color = #FF6EC7>14. char[] toCharArray( );</font>***

***<font color = #00FFFF>> 将字符串转换为 字符数组 </font>***

```java

String str = new String("hello world");

char[] chars = str.toCharArray();

```

<br>

## ***<font color = #FF6EC7>15. String toLowerCase( );</font>***

***<font color = #00FFFF>> 所有字符转换成小写 </font>***

>- ***<font color = #FF6EC7> String toUpperCase( );</font>***

>

>***所有字符转换成大写***

<br>

## ***<font color = #FF6EC7>16. String trim( );</font>***

***<font color = #00FFFF>> 去除字符串的前后空白 </font>***

<br>

## ***<font color = #BC1717>17.static String valueOf( Object obj );</font>***

***<font color = #00FFFF>> 将非字String类型转换为String类型 </font>***

- ### ***<font color = #FF6EC7>Valueof 方法是静态的 直接用 String.valueOf( obj ) 调用</font>***<br>

<br>

- **valueOf 方法在执行时 会调用传入参数对象的 toSting 方法 , 这时需要 重写参数对象的 toString 方法**

```java

class Person{

public String toString(){

return "我变成Sting类型对象了";

}

}

Person p = new Person();

String str = String.valueOf(p);

System.out.println( str ); //自动调用str的toString 方法

```

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值