java常见类之String类

梦伟同学_6080

梦伟同学_6080

2016-10-09

2584人浏览

原创

1.字符串概述

  字符串:就是由多个字符组成的一串字符,也可以看成是字符数组。

  String类代表字符串,java程序中的字符串字面值,如"abc"等都作为此类的实例实现。

  字符串是常量,一旦被赋值,就不能被改变。


2.String的构造方法

  public String() 空构造

  public String(byte[] bytes) 把字节数组转换成字符串

  public String(byte[] bytes,int offset,int length) 把字节数组的指定索引长度的字节转换成字符串

  public String(char[] value) 把字符数组转换成字符串

  public String(char[] values,int offset,int count) 把字符数组的指定索引长度的字符转换成字符串

  public String(String original) 把字符串常量值转换成字符串

package cn;
/**
 *  字符串:就是由多个字符组成的一串字符,也可以看成是字符数组
 * 通过查看API,我们可以知道
 *   字符串字面值"abc"也可以看成是一个字符串对象。
 *      字符串是常量,一旦被赋值,就不能被改变。
 *   
 *   构造方法:
 *   public String() 空构造
 *   public String(byte[] bytes) 把字节数组转换成字符串
 *   public String(byte[] bytes,int offset,int length) 
     把字节数组的指定索引长度的字节转换成字符串
 *   public String(char[] value) 把字符数组转换成字符串
 *   public String(char[] values,int offset,int count) 
     把字符数组的指定索引长度的字符转换成字符串
 *   public String(String original) 把字符串常量值转换成字符串
 *   
 *   字符串的方法:
 *   public int length()获取字符串的长度
 */
public class StringDemo {
    public static void main(String[] args) {
        //public String() 空构造
        String s1 = new String();
        System.out.println("s1:"+s1);//s1:
         
        //public String(byte[] bytes) 把字节数组转换成字符串
        byte[] bytes1 = new byte[]{97,98,99};
        String s2 = new String(bytes1);
        System.out.println("s2:"+s2);//s2:abc
         
        //public String(byte[] bytes,int offset,int length)
        // 把字节数组的指定索引长度的字节转换成字符串
        byte[] bytes2 = new byte[]{97,98,99,100};
        String s3 = new String(bytes2,1,2);
        System.out.println("s3:"+s3);//s3:bc
         
        //public String(char[] value) 把字符数组转换成字符串
        char[] char1 = new char[]{'a','b','c'};
        String s4 = new String(char1);
        System.out.println("s4:"+s4);//s4:abc
         
        //public String(char[] values,int offset,int count)
        // 把字符数组的指定索引长度的字符转换成字符串
        char[] char2 = new char[]{'a','b','c','d','e'};
        String s5 = new String(char2,1,2);
        System.out.println("s5:"+s5);//s5:bc
         
        //public String(String original) 把字符串常量值转换成字符串
        String s6 = new String("HelloWorld");
        System.out.println("s6:"+s6);//s6:HelloWorld
         
         
         
         
    }
}

3.字符串的特点:一旦被声明,就不能被改变

package com;
/**
 *    字符串的特点:一旦被赋值,就不能被改变。 
 *      指的是字符串"hello"不可以改变 吗,而不是变量s
 * 
 * 字符串直接赋值的方式是先到字符串常量池里面去找,
 * 如果有就直接返回,没有,就创建并返回。
 *  
 */
public class StringDemo {
    public static void main(String[] args) {
        String s = "hello";
        s += "world";
        System.out.println("s:"+s);//s:helloworld
    }
}

1.png

4.String直接赋值和创建对象方式的区别

package com;
/**
 *  String s1 = "hello";和 String s2 = new String("hello")的区别
 *  前者会创建2个对象,后者会创建一个对象。
 *  
 *  ==在基本类型中,比较的是值是否相等。在引用类型中,比较的是地址值是否相等。
 *  equals()在Object类中比较的是地址值是否相等。
 *   但是String重写了equals()方法,所以在String比较的是内容是否相等。
 */
public class StringDemo2 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");
         
        System.out.println(s1 == s2);//false
        System.out.println(s1.equals(s2));//true
    }
 
}

2.png

5.练习

package com;
/**
 * 看程序写结果
 */
public class StringDemo3 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2);//false
        System.out.println(s1.equals(s2));//true
         
        String s3 = new String("hello");
        String s4 = "hello";
        System.out.println(s3 == s4);//false
        System.out.println(s3.equals(s4));//true
         
        String s5 = "hello";
        String s6 = "hello";
        System.out.println(s5 == s6 );//true
        System.out.println(s5.equals(s6));//true
    }
 
}
package com;
/**
 * 看程序,写结果
 * 字符串如果是变量相加,先开辟空间,再拼接
 * 字符串如果是常量相加,是先相加,然后再在常量池中寻找,如果有就直接返回,否则,就创建
 */
public class StringDemo4 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";
        System.out.println(s1.hashCode()+s2.hashCode());
        System.out.println(s3.hashCode());
        /**
         * s3 就相当于是s3.hashCode()
         * 而s1 + s2 就相当于s1.hashCode() + s2.hashCode()
         * 当然不一样了
         */
        System.out.println(s3 == s1 + s2);//false
        System.out.println(s3.equals(s1 + s2));//true
         
         
        System.out.println(s3 == "hello"+"world");//true
        System.out.println(s3.equals("hello"+"world"));//true
         
        /**
         * 通过反编译,看源码,我们知道这里已经做好了处理
         */
        System.out.println(s3=="helloworld");
        System.out.println(s3.equals("helloworld"));
         
         
         
    }
 
}

6.String类的判断功能

public boolean equals(Object obj) 判断两个字符串的内容是否相等,区分大小写

public boolean equalsIgnoreCase(String str) 判断两个字符串是否相等,不区分大小写

public contains(String str) 判断大的字符串中是否包含有小的字符串

public boolean stratsWith(String str) 判断字符串是否以某个指定的字符串开头

pubilc booelan endsWithd(String str) 判断字符串是否以某个指定的字符串结尾

public boolean isEmpty() 判断字符串的内容是否为空

package cn;
/**
 * String类的判断功能
 *    public boolean equals(Object obj) 判断字符串的内容是否相等,区分大小写
 *    public boolean equalsIgnoreCase(String str) 判断字符串的内容是否相等,不区分大小写
 *  public contains(String str) 判断大的字符串中是否包含小的字符串
 *  public boolean startsWith(String str) 判断是否以某种字符串开开头
 *  public boolean endsWith(String str) 判断是否以某种字符串结尾
 *  public boolean isEmpty() 判断字符串的内容是否为空
 *
 */
public class StringDemo2 {
    public static void main(String[] args) {
        //创建字符串对象
        String s1 = "helloworld";
        String s2 = "helloworld";
        String s3 = "helloWorld";
         
        //public boolean equals(Object obj) 判断字符串的内容是否相等,区分大小写
        System.out.println(s1.equals(s3));//false
        System.out.println(s1.equals(s2));//true
         
        //public boolean equalsIgnoreCase(String str) 判断字符串的内容是否相等,不区分大小写
        System.out.println(s1.equalsIgnoreCase(s3));//true
        System.out.println(s1.equalsIgnoreCase(s2));//true
         
        //public contains(String str) 判断大的字符串中是否包含小的字符串
        System.out.println(s1.contains("hello"));//true
        System.out.println(s1.contains("hw"));//false
         
        //public boolean startsWith(String str) 判断是否以某种字符串开开头
        System.out.println(s1.startsWith("hello"));//true
        System.out.println(s1.startsWith("h"));//true
        System.out.println(s1.startsWith("world"));//false
         
        //public boolean endsWith(String str) 判断是否以某种字符串结尾
        System.out.println(s1.endsWith("world"));//true
 
        //public boolean isEmpty() 判断字符串是否为空
        System.out.println(s1.isEmpty());//false
        String s4 = "";
        System.out.println(s4.isEmpty());//true
         
        String s5 = null;
        /**
         * Exception in thread "main" java.lang.NullPointerException
         * 因为s5的对象不存在,它怎么可以调用方法呢??
         */
        //System.out.println(s5.isEmpty());
         
    }
 
}

7.练习

package cn;
 
import java.util.Scanner;
 
/**
 * 
 * 模拟登录,给三次机会,并提示还有几次机会
 * 
 * 分析:
 *    1.定义用户名和密码,已经存在的
 *    2.键盘录入用户名和密码。
 *  3.比较用户名和密码,如果都相同,则登录成功,如果有一个不同,则登录失败
 *  4.给三次机会,用循环改进,用for循环
 */
public class StringDemo1 {
    public static void main(String[] args) {
        //定义用户名和密码,已经存在的
        String username = "admin";
        String password = "admin";
         
        for (int i = 0; i <p>改进</p><pre class="brush:java;toolbar:false">package cn;
 
import java.util.Scanner;
 
/**
 * 
 * 模拟登录,给三次机会,并提示还有几次机会,如果登录成功,就可以玩猜数字了
 * 
 * 分析:
 *    1.定义用户名和密码,已经存在的
 *    2.键盘录入用户名和密码。
 *  3.比较用户名和密码,如果都相同,则登录成功,如果有一个不同,则登录失败
 *  4.给三次机会,用循环改进,用for循环
 */
public class StringDemo1 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        //定义用户名和密码,已经存在的
        String username = "admin";
        String password = "admin";
         
        boolean flag = false;
         
        for (int i = 0; i  number){
                System.out.println("你猜的数据"+guessNumber+"大了");
            }else if (guessNumber <p>8.String类的获取功能</p><p>public int length() 获取字符串的长度</p><p>public char charAt(int index) 获取指定索引位置上的字符</p><p> 	为什么这里是int类型,而不是char类型?</p><p>  	因为:'a'和97是可以相互转换的,即97就是代表'a'</p><p>public int indexOf(int ch) 返回指定字符在此字符串中第一次出现的位置</p><p>public int indexOf(String str) 返回指定字符串在此字符串中第一次出现的位置</p><p>public int indexOf(int ch,int fromIndex)返回指定字符在此支付串从指定位置中第一次出现的位置</p><p>public int indexOf(Sring str,itn fromIndex) 返回指定字符串在此字符串从指定位置中第一次出现的位置</p><p>public String subString(int start) 从指定位置开始截取的字符串</p><p>public String subString(int strat,int end) 从指定位置开始到指定位置结束时截取的字符串</p><pre class="brush:java;toolbar:false">package cn;
/**
 *    String类的获取功能
 *  public int length() 获取字符串的长度
 *  public char charAt(int index) 获取指定索引位置上的字符
 *   为什么这里是int类型,而不是char类型?
 *   因为:'a'和97是可以相互转换的,即97就是代表'a'
 *  public int indexOf(int ch) 返回指定字符在此字符串中第一次出现的位置
 *  public int indexOf(String str) 返回指定字符串在此字符串中第一次出现的位置
 *  public int indexOf(int ch,int fromIndex)返回指定字符在此支付串从指定位置中第一次出现的位置
 *  public int indexOf(Sring str,itn fromIndex) 返回指定字符串在此字符串从指定位置中第一次出现的位置
 *  public String subString(int start) 从指定位置开始截取的字符串
 *  public String subString(int strat,int end) 从指定位置开始到指定位置结束时截取的字符串
 */
public class StringDemo4 {
    public static void main(String[] args) {
        //定义一个字符串
        String s = "helloworld";
         
        //public int length() 获取字符串的长度
        System.out.println("s的长度是:"+s.length());//s的长度是:10
         
        //public char charAt(int index) 获取指定索引位置上的字符
        System.out.println(s.charAt(0));//h
         
        //public int indexOf(int ch) 返回指定字符在此字符串中第一次出现的位置
        System.out.println("l出现第一次的索引"+s.indexOf('l'));//l出现第一次的索引2
         
        //public int indexOf(String str) 返回指定字符串在此字符串中第一次出现的位置
        System.out.println("hello第一次出现的索引"+s.indexOf("hello"));//hello第一次出现的索引0
         
        //public int indexOf(int ch,int fromIndex)返回指定字符在此支付串从指定位置中第一次出现的位置
        System.out.println("l从索引为4开始出现d的第一次索引位置:"+s.indexOf('l', 4));//l从索引为4开始出现d的第一次索引位置:8
        System.out.println("l从索引为4开始出现d的第一次索引位置:"+s.indexOf('k', 4));//l从索引为4开始出现d的第一次索引位置:-1
        System.out.println("l从索引为4开始出现d的第一次索引位置:"+s.indexOf('l', 40));//l从索引为4开始出现d的第一次索引位置:-1
         
        //public int indexOf(Sring str,itn fromIndex) 返回指定字符串在此字符串从指定位置中第一次出现的位置
        System.out.println("or从第2个索引位置开始出现的第一个索引位置:"+s.indexOf("or", 2));//or从第2个索引位置开始出现的第一个索引位置:6
         
        //public String subString(int start) 从指定位置开始截取的字符串
        System.out.println("截取world:"+s.substring(5));//截取world:world
        System.out.println("截取helloworld:"+s.substring(0));//截取helloworld:helloworld
         
        //public String subString(int strat,int end) 从指定位置开始到指定位置结束时截取的字符串
        System.out.println("截取wo:"+s.substring(5, 7));//截取wo:wo
         
         
         
    }
 
}

9.练习

package cn;
/**
 * 需求:遍历获取字符串中的每一个字符
 *
 */
public class StringTest {
    public static void main(String[] args) {
        String s = "helloworld";
        //第一种实现方法
        for (int i = 0; i <pre class="brush:java;toolbar:false">package cn;
/**
 *  需求:统计一个字符串中大写字母字符、小写字母字符,数字字符出现的次数(不考虑其他字符)
 *  举例:
 *   "Hello123World"
 *  结果:
 *   大写字符 2个
 *   小写字符 8个
 *   数字字符 3个
 *  
 *
 */
public class StringTest2 {
    public static void main(String[] args) {
        String str = "Hello123World";
        int maxCharacterSum = 0;
        int minCharacterSum = 0;
        int numSum = 0;
         
        //方法一:将字符串转换为字符数组
        char[] chs = str.toCharArray();
        for (int i = 0; i = 'a' && chs[i] = 'A' && chs[i] = '0' && chs[i] = 'a' && ch = 'A' && ch = '0' && ch <p>10.String类的转换功能</p><p>public byte[] getBytes() 字符串转换为字节数组</p><p>public char[] toCharArray() 字符串转换为字符数组</p><p>public static String valueOf(char[] chs) 将字符数组转换为字符串</p><p>public static String valueOf(int i) 将int类型的数值转换为字符串</p><p>public String toLowerCase() 将字符串转换为小写</p><p>public String toUpperCase() 将字符串转换为大写</p><p>public String concat(String str) 字符串的拼接</p><pre class="brush:java;toolbar:false">package cn;
/**
 * String类的转换功能
 * 
 * public byte[] getBytes() 字符串转换为字节数组
 * public char[] toCharArray() 字符串转换为字符数组
 * public static String valueOf(char[] chs) 将字符数组转换为字符串
 * public static String valueOf(int i) 将int类型的数值转换为字符串
 * public String toLowerCase() 将字符串转换为小写
 * public String toUpperCase() 将字符串转换为大写
 * public String concat(String str) 字符串的拼接
 *
 */
public class StringDemo5 {
    public static void main(String[] args) {
        //定义一个字符串对象
        String s = "javaSE";
         
        //public byte[] getBytes() 字符串转换为字节数组
        byte[] bytes = s.getBytes();
        for (int i = 0; i <p>11.练习</p><pre class="brush:java;toolbar:false">package cn;
/**
 * 需求:把一个字符串的首字母转换为大写
 * 举例:
 *    helloWORLD
 * 结果
 *  HelloWORLD
 */
public class StringTest3 {
    public static void main(String[] args) {
        String str = "helloWORLD";
        str = str.substring(0,1).toUpperCase()+str.substring(1);
        System.out.println(str);//HelloWORLD
    }
 
}
package cn;
/**
 * 需求:把一个字符串的首字母转换为大写,其余为小写
 * 举例:
 *    helloWORLD
 * 结果
 *  Helloworld
 */
public class StringTest3 {
    public static void main(String[] args) {
        String str = "helloWORLD";
        str = str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase();
        System.out.println(str);//Helloworld
    }
 
}

本文出自 “11831428” 博客,请务必保留此出处http://11841428.blog.51cto.com/11831428/1859607

Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

20

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

60

15

GoFrame框架零基础入门
GoFrame框架零基础入门

本专题整理GoFrame框架的入门知识,从模块化设计理念、环境准备到跑通第一个Web服务,讲清目录分层、配置加载、日志与中间件的用法,并对比Gin、Go-Zero的差异,帮助Go开发者建立整体认知。

2026.11.17

60

15

SQLAlchemy 一对多关系怎么建立
SQLAlchemy 一对多关系怎么建立

SQLAlchemy 一对多关系怎么建立?本专题整理父子表外键、relationship 双向映射、关联查询、级联删除和加载优化等相关文章,帮助你理解并实现常见的 ORM 关系建模。

2026.11.17

20

10

SQLAlchemy 如何增删改查
SQLAlchemy 如何增删改查

SQLAlchemy 如何增删改查?本专题整理 ORM 新增记录、条件查询、更新和删除、Session 事务提交与批量处理等实用文章,帮助你掌握 Python 数据库操作流程。

2026.11.17

60

11

SQLAlchemy ORM 模型怎么写
SQLAlchemy ORM 模型怎么写

SQLAlchemy ORM 模型怎么写?本专题整理模型类定义、Mapped 字段映射、主键与约束、表结构创建和常见报错的相关文章,帮助你从基础配置到实际建模逐步掌握 SQLAlchemy。

2026.11.17

40

16

Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

180

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

140

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

160

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.3万人学习