Java中String的常用方法总结

1、length()字符串的长度

  String str="HelloWord";
System.out.println(str.length());

输出结果是10

2、charAt() 截取一个字符

3 getchars()截取多个字符并由其他字符串接收

4 getBytes()将字符串变成一个byte数组

5 toCharArray()将字符串变成一个字符数组

6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分

 

7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束

8 toUpperCase()和toLowerCase()将字符串转换为大写或小写

9 concat() 连接两个字符串

10 trim()去掉起始和结束的空格

11 substring()截取字符串

12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方

13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分

14 replace() 替换

代码演示:

 package com.aaa.demo9;

 public class StringDemo {
public static void main(String[] args) { // 2、charAr() 截取一个字符
String charAtDemo="hello";
System.out.println(charAtDemo.charAt(2));
//输出的是l //3 getchars()截取多个字符并由其他字符串接收
String getChars="hello";
char[] getCharDemo=new char[10];
getChars.getChars(0, 3, getCharDemo, 0);
//第一个数值代表的是截取的字符串开始的位置
//第二个数值代表截取要截取的字符串的结束后的下一个下标(也可以理解为截取的长度)
//第三个代表的是接收的字符串数组,最后一个参数是接收数组的开始位置。
System.out.println(getCharDemo); //4 getBytes()将字符串变成一个byte数组
String getByte="hello";
byte[] b=getByte.getBytes();
System.out.println(new String(b)); //5 toCharArray()将字符串变成一个字符数组
String charArr="hello";
char[] c = charArr.toCharArray();
System.out.println(c); //6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分
String equalA="hello";
String equalB="hello";
boolean equals = equalA.equals(equalB);
System.out.println(equals);
//结果为true //7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束
String startA="hello.java";
boolean endsWith = startA.endsWith(".java");
System.out.println(endsWith);
//结果为true //8 toUpperCase()和toLowerCase()将字符串转换为大写或小写
String upperCase="Hllo";
String upp = upperCase.toUpperCase();
System.out.println(upp);
//输出结果为HLLO //9 concat() 连接两个字符串
String concatA="hello";
String concatB="你好";
String concat = concatA.concat(concatB);
System.out.println(concat); //10 trim()去掉起始和结束的空格
String trimA=" hello ";
System.out.println(trimA.trim()); //11 substring()截取字符串
String subA="hello";
System.out.println(subA.substring(1)); //12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方
String indexA="hello";
System.out.println("index"+indexA.indexOf("e"));//返回的是下标的值,如果找不到返回-1 //13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分
String comA="hello";
String comB="Word";
System.out.println(comA.compareTo(comB)); //14 replace() 替换
String reA="hello";
reA=reA.replace("e", "a");
System.out.println(reA); }
}

Java中String的常用方法总结的更多相关文章

  1. java中String的常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...

  2. JAVA中String类常用方法 I

    String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...

  3. Java 中 String 的常用方法(一)

    上一篇介绍了 String 中的几个常用构造方法,由 String 这个核心对象发散出去关于字符的编码,字符的字节表达,对 GC 的影响,正则表达式,模式匹配,这可能是 Java 里内涵最丰富的对象了 ...

  4. Java中String类常用方法(字符串中的子字符串的个数)

    重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...

  5. Java 中 String 的常用方法(二)

    本文介绍剩下的一些常用的 String 中的方法. 1.replace 方法 .replaceFirst 方法和 replaceAll 方法 replace(char oldChar, char ne ...

  6. Java 中String常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...

  7. Java中String常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...

  8. java基础——String的常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); i nt le ...

  9. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

随机推荐

  1. 《算法》第四章部分程序 part 6

    ▶ 书中第四章部分程序,加上自己补充的代码,图的环相关 ● 无向图中寻找环 package package01; import edu.princeton.cs.algs4.In; import ed ...

  2. [多线程]wait和notify

    线程之间的通信   使用wait/notify方法实现线程间的通信.这两个方法都是Object类的方法,也就是说Java所有的对象都提供这两个方法.1.wait和notify必须配合synchroni ...

  3. 序列化 (实现RPC的基础)

    public interface ISerializer { <T> byte[] serializer(T obj); <T> T deSerializer(byte[] d ...

  4. ssm学习的第一个demo---crm(4)

    (1)在crm系统中点击修改弹出了修改的框,这个使用bootstrap做的,然后看jsp代码,找到 用editCustomer,按Ctrl+k找到了次函数,edit.action没有,创建 (2)去持 ...

  5. JSR303 分組数据验证的使用

    场景:一个Bean ,需要在不同情况下分别做验证 1.依赖:springboot 已经集成 2.定义一个bean (验证对象) import javax.validation.constraints. ...

  6. Redis位图实现用户签到功能

    场景需求 适用场景如签到送积分.签到领取奖励等,大致需求如下: 签到1天送1积分,连续签到2天送2积分,3天送3积分,3天以上均送3积分等. 如果连续签到中断,则重置计数,每月初重置计数. 当月签到满 ...

  7. JEECG 3.7.3 新春版本发布,企业级JAVA快速开发平台

    JEECG 3.7.3新春版本发布 -  微云快速开发平台 导读           ⊙精美Echart报表 ⊙二维码生成功能 ⊙Online接口改造采用JWT机制 ⊙智能菜单搜索 ⊙代码生成器模板优 ...

  8. 现学现卖——Keil uVision 使用教程

    Keil uVision 使用教程 1.如果有旧的工程在,先关闭旧工程.Project -> Close Project2.新建工程.Project -> New uVision Proj ...

  9. Navicat Premium 连接Oracle 数据库(图文教程)

    一.需要准备的软件 Navicat premium 32位 官方下载地址:http://www.navicat.com.cn/products/navicat-premium Instant Clie ...

  10. 企业应用--web环境部署于上线流程

    服务器逻辑 1.服务器: 2.操作系统: 3.部署逻辑: 测试环境部署 预发布系统: 线上业务服务器部署 业务环境部署逻辑 测试: 上线: