Java中String的常用方法总结
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的常用方法总结的更多相关文章
- java中String的常用方法
java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...
- JAVA中String类常用方法 I
String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...
- Java 中 String 的常用方法(一)
上一篇介绍了 String 中的几个常用构造方法,由 String 这个核心对象发散出去关于字符的编码,字符的字节表达,对 GC 的影响,正则表达式,模式匹配,这可能是 Java 里内涵最丰富的对象了 ...
- Java中String类常用方法(字符串中的子字符串的个数)
重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...
- Java 中 String 的常用方法(二)
本文介绍剩下的一些常用的 String 中的方法. 1.replace 方法 .replaceFirst 方法和 replaceAll 方法 replace(char oldChar, char ne ...
- Java 中String常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len ...
- Java中String常用方法
java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...
- java基础——String的常用方法
java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); i nt le ...
- Java中String类的方法及说明
String : 字符串类型 一. String sc_sub = new String(c,3,2); // String sb_copy = new String(sb) ...
随机推荐
- 《C++数据结构-快速拾遗》 基础常识
1.命名空间函数 namespace wjy { void print() { cout<<"; } int load(int num) { return num; } } us ...
- embsysregview 0.26 无法安装的解决方法。
最近看到一个比较好的 eclipse 插件:embsysregview,于是想装起来用用看.结果安装过程出错,4个 jar 的包下载不下来,并且通过本地安装的方法也不行. 后来终于找到作者的回复,作者 ...
- SAPCertifiedTechnologyAssociate-SystemAdministration(SAPHANAasaDatabase)
C_TADM55_75 SAP Certified Technology Associate - System Administration (SAP HANA as a Database) with ...
- 《算法》第四章部分程序 part 19
▶ 书中第四章部分程序,包括在加上自己补充的代码,有边权有向图的邻接矩阵,FloydWarshall 算法可能含负环的有边权有向图任意两点之间的最短路径 ● 有边权有向图的邻接矩阵 package p ...
- Flex学习笔记-使用MXML和一个AS事件监听器监听事件
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- centos 7 端口
查看端口是否占用 netstat -tlnp|grep 8080 查看已经开放的端口 firewall-cmd --zone=public --list-ports 增加开放端口 firewall-c ...
- python 如何获取当前文件/文件夹
python 如何获取当前文件/文件夹? 1.获取当前文件的实际路劲: os.path.realpath(__file__) ==> D:\python_test\test_p ...
- iOS响应链原理
ios找到被点击的view的过程是从根view开始递归地调用hitTest方法,直到有一个子view的hitTest方法返回自身:如果所有一级子view的hitTest方法都返回nil,那么根view ...
- 安装安卓SDK和JDK的简便方法
直接在VS的安装程序里选:使用.NET的移动开发,其中就包括了安卓SDK,JAVA SE等 另外:自己手动安装SDK时,不要选模拟器相关的东西,太大了,如果每个版本都选,安装下来上100G以上
- Linux安装卸载jdk1.8
首先到官网下载 Linux x64 182.87 MB jdk-8u191-linux-x64.tar.gz https://www.oracle.com/technetwork/java/java ...