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. GNU C语言开发环境

    1. GNU C 编译器 2. GNU make 项目管理工具 3. 创建和使用函数库 4. GNU C 函数库(glibc) 1.GNU C 编译器 使用 c语言 编写的代码,运行前必须经过编译和链 ...

  2. Java swing 代码例子

    package com; import java.awt.Button; import java.awt.Container; import java.awt.event.ActionEvent; i ...

  3. Python——列表深浅拷贝

    一.深浅拷贝 如果希望将列表复制一份,通过列表的内置方法copy就可以实现: s = [[1,2],3,4] s1 = s.copy() print(s) print(s1) 拷贝出的列表s1与原列表 ...

  4. JavaScript Object 对象

    Object 对象自身用处不大,不过在了解其他类之前,还是应该了解它.因为 ECMAScript 中的 Object 对象与 Java 中的 java.lang.Object 相似,ECMAScrip ...

  5. mongodb的副本集|备份|恢复备份

    复制(副本集) 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备份 数据灾 ...

  6. 4. Tomcat内存溢出解决

    1.  java.lang.OutOfMemoryError: PermGen space a.如果tomcat是以bat方式启动的,则如下设置: 修改TOMCAT_HOME/bin/catalina ...

  7. 使用STM32的USART的同步模式Synchronous调戏SPI[2] 【实现spi 9bit】

    [原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/7064533.html 上回说道使用USART的来模拟SPI通讯.说说一下我什么写这个的原 ...

  8. 机器学习进阶-图像金字塔与轮廓检测-模板匹配(单目标匹配和多目标匹配)1.cv2.matchTemplate(进行模板匹配) 2.cv2.minMaxLoc(找出矩阵最大值和最小值的位置(x,y)) 3.cv2.rectangle(在图像上画矩形)

    1. cv2.matchTemplate(src, template, method)  # 用于进行模板匹配 参数说明: src目标图像, template模板,method使用什么指标做模板的匹配 ...

  9. day01-MySQL介绍

    一.MySQL的介绍 1.1.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 W ...

  10. VsCode 使用习惯设置(备份)

    { "window.menuBarVisibility": "toggle", "workbench.statusBar.visible": ...