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. Visual Studio 2017-2019版本创建C#项目时没有创建网站这一选项?

    通过了解以后发现Visual Studio 2017之后的版本在新建选项中已经不再有这一选择项了. 解决办法: 1.在创建新项目的面板滑倒最下面,---> 安装多个人工具和功能 2.这时已经打开 ...

  2. IP网络主动测评系统

    一.IT网络运维面临的挑战 1. 网络性能可视化 • 与公有云和SaaS平台连接的可靠性 • 广域网线路性能 • 互联网专线性能 2.诊断工具 • 现场无IT工程师覆盖 • 诊断的人力费用 • 网络与 ...

  3. python为什么是高级语言和解释型编程语言?它是如何粘合其它语言写的代码的?

    学习python之初,不知道大家对于python有没有疑惑,应当是有的.这里我整理出来了自己的一些疑惑,供大家参考. 为什么python是高级程序设计语言 ​ Java,C,C++这些语言是高级语言, ...

  4. C# 事件Event(个人整理)

    内容来源:MSN:https://docs.microsoft.com/zh-cn/dotnet/csharp/event-pattern 操作符详解(上)   https://www.youtube ...

  5. 接口java.util.Map的四个实现类HashMap Hashtable LinkedHashMap TreeMap

    java中HashMap,LinkedHashMap,TreeMap,HashTable的区别 :java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMa ...

  6. 深入Mybatis框架

    深入Mybatis框架 学习了Spring之后,我们已经了解如何将一个类作为Bean交由IoC容器管理,也就是说,现在我们可以通过更方便的方式来使用Mybatis框架,我们可以直接把SqlSessio ...

  7. Centos6.x安装之后的9件事

    Centos6.x安装之后的9件事 这些不是必须都做的,只不过是我个人的习惯,在此记录一下. 1.修改yum源到国内 CentOS系统更换软件安装源 备份你的原镜像文件,以免出错后可以恢复. mv / ...

  8. elementUI日期选择器 el-date-picker根据所选日期选择禁用

    我遇到这样一个场景,需要动态渲染时间表单,但是后端传过来的数据, 这个时候就不能预先找到想要限制的date了,因为连date本身,也是根据后端传来的数据生成的. 如图: 代码如下: //templat ...

  9. c语言刷 队列题记录

    622. 设计循环队列 https://blog.csdn.net/Galaxy_n/article/details/115978544 typedef struct { int *arrs; int ...

  10. 开机弹出一下Visual Studio Just-In-Time对话框的问题

    开机弹出一下Visual Studio Just-In-Time对话框 开机弹出一下Visual studio just-in-time对话框,出现一下问题,且点击确定后又弹出第二个对话框, 解决方法 ...