package com.javaBase.string;

import java.util.Locale;

/**
* 〈一句话功能简述〉;
* 〈String类中常用的方法整理〉
*
* @author
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class CommonStringMethod { public static void main(String[] args) {
// testLength();
// testChartAt();
// testToCharyArray();
// testIndexOf();
// testToUpperCaseAndToLowerCase();
// testSplit();
// testTrim();
// testSubstring();
// testEqualsIgnoreCase();
// testContains();
// testStartsWithAndEndsWith();
testReplaceAll();
} /**
* length() 返回此字符串的长度。长度等于字符串中 Unicode 代码单元的数量。
* 返回值为 int 类型。得到一个字符串的字符个数(中、英、空格、转义字符皆为字符,计入长度)
*/
public static void testLength() {
String str = "123456 \t \n";
System.out.println(str.length());
} /**
* charAt(index);返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值位于索引 0 处,
* 第二个位于索引 1 处,依此类推,这类似于数组索引。
*/
public static void testChartAt() {
String str = "123456";
System.out.println(str.charAt(0));
System.out.println(str.charAt(1));
} /**
* toCharArray();将此字符串转换为一个新的字符数组。
*/
public static void testToCharyArray() {
String str = "123456";
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]);
} /**
* indexOf(int ch);返回指定字符在此字符串中第一次出现处的索引
* indexOf(int ch, int fromIndex);返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
* indexOf(String str);返回指定子字符串在此字符串中第一次出现处的索引
* indexOf(String str, int fromIndex);返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
*/
public static void testIndexOf() {
String str = "123456";
System.out.print(str.indexOf('1'));
System.out.print(str.indexOf('1',1));
System.out.print(str.indexOf("1"));
System.out.print(str.indexOf("1",1));
} /**
* toLowerCase();使用默认语言环境的规则将此 String 中的所有字符都转换为小写
* toLowerCase(Locale locale);使用给定 Locale 的规则将此 String 中的所有字符都转换为小写
* toUpperCase();使用默认语言环境的规则将此 String 中的所有字符都转换为大写
* toUpperCase(Locale locale);使用给定 Locale 的规则将此 String 中的所有字符都转换为大写
*/
public static void testToUpperCaseAndToLowerCase() {
String str = "123456";
System.out.println(str.toUpperCase()); //123456
String str2 = "奥术大师多";
System.out.println(str2.toUpperCase()); //奥术大师多
String str3 = "abcA";
System.out.println(str3.toUpperCase());
System.out.println(str3.toUpperCase(Locale.CANADA_FRENCH)); //了解下local类
System.out.println(str3.toLowerCase());
} /**
* split();根据给定正则表达式的匹配拆分此字符串。
*/
public static void testSplit() {
String str = "123456";
String[] arr = str.split(",");
for (int i=0;i<arr.length;i++)
System.out.println(arr[i]);
} /**
* equals(Object anObject);将此字符串与指定的对象比较。当且仅当该参数不为 null,
* 并且是与此对象表示相同字符序列的 String 对象时,结果才为 true
*/
public static void testEquals() {
String str = "123456";
System.out.println("123".equals(str));
} /**
* trim();返回字符串的副本,忽略前导空白和尾部空白。
* replace(char oldChar, char newChar) ;返回一个新的字符串,它是通过用 newChar
* 替换此字符串中出现的所有 oldChar 得到的。
*/
public static void testTrim() {
String str = " 12 3456 ";
System.out.println(str.trim()); //12 3456
System.out.println(str.replace(" ","")); //123456
} /**
* substring(int beginIndex);返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,
* 直到此字符串末尾。
* substring(int beginIndex,int endIndex);返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的
* beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
*/
public static void testSubstring() {
String str = "123456";
System.out.println(str.substring(2));
System.out.println(str.substring(2,4)); //左闭右开
} /**
* equalsIgnoreCase(String anotherString);将此 String 与另一个 String 比较,不考虑大小写
*/
public static void testEqualsIgnoreCase() {
String str = "abc";
System.out.println("AbC".equalsIgnoreCase(str));
} /**
* contains(CharSequence s);当且仅当此字符串包含指定的 char 值序列时,返回 true。
*/
public static void testContains() {
String str = "abcdef123";
System.out.println(str.contains("f12"));
} /**
* startsWith(String prefix);测试此字符串是否以指定的前缀开始。
* endsWith(String suffix) ;测试此字符串是否以指定的后缀结束。
*/
public static void testStartsWithAndEndsWith() {
String str = "abcdef123";
System.out.println(str.endsWith("123"));
System.out.println(str.startsWith("123"));
} /**
* replaceAll(String regex, String replacement);使用给定的 replacement 替换此字符串
* 所有匹配给定的正则表达式的子字符串。
*/
public static void testReplaceAll() {
String str = "abcdef1231";
str = str.replaceAll("1","@");
System.out.println(str);
String str2 = "abcdef1231";
str2 = str2.replace("1","#");
System.out.println(str2);
//一样?
String str3 = "abcdef1231";
str3 = str3.replaceAll("\\d", "*"); //abcdef**** replaceAll()支持正则表达式
System.out.println(str3);
}
}

String常用方法解析的更多相关文章

  1. Device Tree常用方法解析

    Device Tree常用方法解析 Device Tree在Linux内核驱动中的使用源于2011年3月17日Linus Torvalds在ARM Linux邮件列表中的一封邮件,他宣称“this w ...

  2. Device Tree常用方法解析【转】

    转自:https://blog.csdn.net/airk000/article/details/21345159 Device Tree常用方法解析 Device Tree在Linux内核驱动中的使 ...

  3. Ext.dom.Element 常用方法解析

    Ext.dom.Element 常用方法解析 Ext.Element,Ext.core.Elemen,Ext.dom.Element 这几个类都是一个类,在EXT当中给起了别名而已,这个类到作用主要是 ...

  4. 前端开发:Javascript中的数组,常用方法解析

    前端开发:Javascript中的数组,常用方法解析 前言 Array是Javascript构成的一个重要的部分,它可以用来存储字符串.对象.函数.Number,它是非常强大的.因此深入了解Array ...

  5. Javascript语言精粹之String常用方法分析

    Javascript语言精粹之String常用方法分析 1. String常用方法分析 1.1 String.prototype.slice() slice(start,end)方法复制string的 ...

  6. JAVA之旅(十六)——String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较

    JAVA之旅(十六)--String类,String常用方法,获取,判断,转换,替换,切割,子串,大小写转换,去除空格,比较 过节耽误了几天,我们继续JAVA之旅 一.String概述 String时 ...

  7. String常用方法

    1. String StringBuffer StringBuilder的区别: 001.在执行速度方法 StringBuilder > StringBuffer > String 002 ...

  8. JVM内存分配及String常用方法

    一,JVM内存分配和常量池 ​ 在介绍String类之前,先来简单分析一下在JVM中,对内存的使用是如何进行分配的.如下图所示(注意:在jdk1.8之后便没有方法区了): ​ ​ 如上JVM将内存分为 ...

  9. JavaScript数组常用方法解析和深层次js数组扁平化

    前言 数组作为在开发中常用的集合,除了for循环遍历以外,还有很多内置对象的方法,包括map,以及数组筛选元素filter等. 注:文章结尾处附深层次数组扁平化方法操作. 作为引用数据类型的一种,在处 ...

随机推荐

  1. Abp 异常处理

    Abp 异常处理 最近一直在读代码整洁之道,我在读到第三章函数的3.9 使用异常替代返回错误码,其实在我的开发经历中都是使用返回错误码给到前端,之前在阅读ABP官网文档中就有看到过使用异常替代异常的做 ...

  2. 三大数据库 sequence 之华山论剑 (上篇)

    前言 本文将基于以下三种关系型数据库,对 sequence (序列) 展开讨论. Oracle - 应用最广泛的商用关系型数据库 PostgreSQL - 功能最强大的开源关系型数据库 MySQL - ...

  3. 2021顶级的开源 BI(商业智能)软件和报表工具

    在这个信息化时代,每分每秒都产生海量数据.在海量数据中,挖掘出有用的数据,并且能以较人性化.直观的方式展示这些数据,变得尤为重要.本文将介绍5款顶级开源 BI(商务智能)软件和报表工具,用于商业数据的 ...

  4. Java课程设计---安装Mysql及管理工具

    1.安装mysql 没有安装包的可以在这个地址下载:https://dev.mysql.com/downloads/mysql/5.5.html 双击提供的安装包 (安装路径可以不用更改) 在弹出的窗 ...

  5. redis面试1-33

    目录 1.Redis你比较熟吧,说说它机制为什么快? 2.redis是单线程吗? 3.为什么redis需要把所有数据放到内存中? 4.Redis的回收策略有哪些? 5.MySQL里有2000w数据, ...

  6. HTML分块

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>菜鸟 ...

  7. Pandas:DataFrame绘制并保存折线图时不打开图形只保存文件

    保存图形,用的是plt.savefig函数,只需要在保存图形之后,调用plt.close()关闭画布,就不会显示出来了: data.plot() outfile='image.png' plt.sav ...

  8. Go标准的目录结构(自总结)

    微服务版 ├── LICENSE.md ├── Makefile //在任何一个项目中都会存在一些需要运行的脚本,这些脚本文件应该被放到 /scripts 目录中并由 Makefile 触发 ├── ...

  9. LeetCode-038-外观数列

    外观数列 题目描述:给定一个正整数 n ,输出外观数列的第 n 项. 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述. 你可以将其视作是由递归公式定义的数字字符串序列: ...

  10. LeetCode-057-插入区间

    插入区间 题目描述:给你一个 无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例说明请见LeetCo ...