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. linux系统上面使用tab补全命令

    tab的模块脚本 [root@centos7 ~]# cat tab.py #!/usr/bin/python3.6 # python startup file import sys import r ...

  2. 【Python爬虫实战】pywin32 安装后出现 import win32api ImportError DLL load failed

    windows下执行 scrapy 的指定的时候出现错误, 最初出现错误  提示没有pywin32  那么就去安装了一个pywin32  然后pip安装 https://www.lfd.uci.edu ...

  3. 【Excel技能】字符串包含某字符串个数?替换许多组字符串?

    =len(单元格A)-len(substitute(单元格A,某字符串,)) 原理:将某字符串替换成空,前后字符串长即为减去的这个字符串长度,这个字符串出现个数=前后字符串长度之差/这个字符串长度 = ...

  4. C#利用反射动态调用DLL并返回结果,和获取程序集的信息

    反射的基本概念: .Net Framework 中提供了反射机制,可以再加载程序运行时,动态获取和加载程序集,并且可以获取到程序集的信息 创建Assembly和Entity两个程序集,在Assembl ...

  5. hive设置参数的方法

    1.修改环境变量 ${HIVE_HOME}/conf/hive-site.xml 2.命令行参数 -e : 执行短命令 -f :  执行文件(适合脚本封装) -S : 安静模式,不显示MR的运行过程 ...

  6. 2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found.

    报错信息: 2018-06-25 14:26:17.103  WARN 49752 --- [  restartedMain] ationConfigEmbeddedWebApplicationCon ...

  7. 用JS 和 jQery获取屏幕的高度和宽度

    用的时候,网上找了下,放在一起,方便以后查阅 document.body.clientWidth document.body.offsetWidth(包括线宽)//网页可见区域宽 document.b ...

  8. 自动配置IE代理脚本

    http://www.cnblogs.com/ttyp/archive/2005/11/18/279124.html

  9. Asp.net有三大对象:HttpContext, HttpRequest, HttpResponse

    一.Asp.net有三大对象:HttpContext, HttpRequest, HttpResponse.命名空间:  System.Web程序集:  System.Web(在 System.Web ...

  10. Django 之Form

    具体可参考:http://www.liujiangblog.com/course/django/153 https://www.cnblogs.com/liuguniang/p/7141837.htm ...