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. ubuntu上virtualbox无法找到usb设备【解决】

    How to set up USB for Virtualbox? USB in different versions of Virtual Box For use of USB in Virtual ...

  2. 使用DolphinPHP的框架中的excel插件导入数据

    直接上函数吧 public function importfile() { if ($this->request->isPost()) { if($_POST['files']) { Cu ...

  3. excel导入导出的两种方式:csv和XLS

    依赖 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl ...

  4. Archlinux下vmware-workstation中安装rhel5并通过桥接模式(bridge)上网

    首先安装vmware-workstation(从aur中) $yaourt vmware-workstation 安装linux-headers $pacman -S linux-headers 然后 ...

  5. 20165304 2017-2018-2 《Java程序设计》第3周学习总结

    教材学习总结 类与对象学习总结 1.类:java作为面向对象型语言具有三个特性:①封装性.②继承性.③多态性.java中类是基本要素,类声明的变量叫对象.在类中定义体的函数题叫方法. 2.类与程序的基 ...

  6. Centos7 Minimal 安装后 初始化配置

    安装完成后初始化配置 1:更新yum yum upgrade 2: 安装基础命令 #yum -y install vim* lrzsz gcc-c++ pcre pcre-devel zlib zli ...

  7. English-旅游英语及情景对话

    1.旅游英语:预订机票情景对话及常用句型 目前,越来越多的人都选择以飞机为出行方式.但是如何用一口流利的英语订机票呢?这里我们替你总结了一些情景对话,还有一些常用的句型.大家都来学一学吧~A:Good ...

  8. 03_JSX理解和使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Django权限auth模块详解

    转自:http://www.cnblogs.com/Finley/p/5575305.html 1,auth模块是Django提供的标准权限管理系统,可以提供用户身份认证,用户组和权限管理 2,aut ...

  10. pod install vs pod update

    Podfile文件,Podfile.lock文件 Podfile文件:指定依赖库的版本规则 Podfile.lock文件:记录当前工程中使用的依赖库的版本号 pod install会去安装podfil ...