String类方法的使用
String类的判断功能:
boolean equals(Object obj) //比较字符串内容是否相同(区分大小写)。
boolean equalsIgnoreCase(String str) // 不区分大小写。
boolean contains(String str) // 判断当前字符串中是否包含参数内容。
boolean startsWith(String str) //判断当前字符串对否以参数字符串开头。
boolean endsWith(String str) // 判断当前字符串是否已参数字符串结尾。
boolean isEmpty() //判断空字符串 “”。
public class StringDemo { public static void main(String[] args) { //boolean equals(Object obj) String s = "helloworld"; System.out.println(s.equals("Helloworld")); //不区分大小写的相等比较 System.out.println(s.equalsIgnoreCase("Helloworld")); //boolean contains(String str) // 判断当前字符串中是否包含参数内容 String s1 = "wnagdao"; String s2 = "gd"; System.out.println(s1.contains(s2)); //boolean startsWith(String str) //判断当前字符串对否以参数字符串开头 String s3 = "wn"; System.out.println(s1.startsWith(s3)); //boolean endsWith(String str) // 判断当前字符串是否已参数字符串结尾 String s4 = "ao"; System.out.println(s1.endsWith(s4)); //boolean isEmpty() //判断空字符串 “” null String s5 = ""; System.out.println(s5.isEmpty()); //int a = ' '; //System.out.println(a); } }
String类的的获取功能:
对于所有的下标位置而言: 从0开始递增 > 0
通常对于查找而言 -1表示没找到。
int length() // 当前字符串中包含的字符序列中的字符个数。
char charAt(int index) // 返回当前字符串对象中某个位置的字符“abc”。
int indexOf(int ch) //返回 某个字符在当前字符串中首次出现的位置。
int indexOf(String str) //查找参数字符串,当前字符串中首次出现的位置。
int indexOf(int ch,int fromIndex)
// 从当前字符串的某个位置开始(包含这个位置),向后查找参数字符首次出现的位置。
int indexOf(String str,int fromIndex)
//从当前字符串的某个位置开始(包含这个位置),向后查找参数字符串首次出现的位置。
String substring(int start)
//返回当前字符串从某个位置开始一直到结束的子串(不会改变原字符串)。
String substring(int start,int end) // 从[start, end)位置的当前字符串的子串。
public class StringDemo { public static void main(String[] args) { //int length() // 当前字符串中包含的字符序列中的字符个数 String s1 = "atm"; System.out.println(s1.length()); // char charAt(int index) // 返回当前字符串对象中某个位置的字符 “abc” String s2 = "yuzhoutiao"; System.out.println(s2.charAt(4)); //int indexOf(int ch) // 返回 某个字符在当前字符串中首次出现的位置 String s3 = "absca"; System.out.println(s3.indexOf('a')); // int indexOf(int ch,int fromIndex) // 从当前字符串的某个位置开始(包含这个位置), // 向后查找参数字符首次出现的位置 System.out.println(s3.indexOf('a', 1)); //int indexOf(String str,int fromIndex) String s4 = "sc"; System.out.println(s3.indexOf(s4)); System.out.println(s3.indexOf(s4, 3)); // String substring(int start) String s5 = "abcedef"; System.out.println(s5.substring(2));//cedef //String substring(int start,int end) [start, end) System.out.println(s5.substring(1, s5.length() - 1)); //bcede } }
/* 删除功能 public StringBuffer deleteCharAt(int index) public StringBuffer delete(int start,int end) [start, end) 替换功能 // 用所给字符串,替换掉字符缓冲区中,指定范围的字符串虚列 public StringBuffer replace(int start,int end,String str) 反转功能 public StringBuffer reverse() //反转字符缓冲区中的字符序列 */ public class StringOtherapi { public static void main(String[] args) { String s = "zhang"; StringBuffer stringBuffer = new StringBuffer(s); //测试删除字符 StringBuffer stringBuffer1 = stringBuffer.deleteCharAt(s.length() - 1); System.out.println(stringBuffer1.toString()); //测试删除某个[start, end) StringBuffer delete = stringBuffer.delete(2, s.length()); System.out.println(delete.toString()); //替换功能 StringBuffer abcd = stringBuffer.replace(0, 2, "abcd"); System.out.println(abcd.toString()); //测试字符串反转 String str = reverseStr("abcd"); System.out.println(str); //测试一下 str = "hansh"; StringBuffer stringBuffer1 = new StringBuffer(str); System.out.println(stringBuffer1.reverse().toString()); String path = "/zs/web/file"; String[] split = path.split("/"); for (int i = 0; i < split.length; i++) { System.out.println(split[i]); } } public static String reverseStr(String s) { char[] tmp = new char[s.length()]; int length = s.length(); for (int i = 0; i < length; i++) { char c = s.charAt(i); tmp[length - 1 - i] = c; } return new String (tmp); } }
String类的转化功能:
byte[] getBytes() //返回当前字符串中字符序列所代表的byte[]。
char[] toCharArray() //返回当前字符串中字符序列的字符数组。
static String valueOf(char[] chs) //将字符数组转化成对应的字符串。
static String valueOf(int i) // 把整数表示为其字符串表示形式 1 “1”。
String toLowerCase() //把字符串转化为小写。
String toUpperCase() //把字符串转化为大写。
String concat(String str) //字符串连接。
public class StringDemo { public static void main(String[] args) { //byte[] getBytes() //返回当前字符串中字符序列所代表的byte[] String s = "abc"; System.out.println(Arrays.toString(s.getBytes())); //[B@4554617c // char[] toCharArray() //返回当前字符串中字符序列的字符数组 System.out.println(Arrays.toString(s.toCharArray())); //static String valueOf(char[] chs) //将字符数组转化成对应的字符串 char[] chars = {'a', 'b', 'c', 'd'}; String s1 = String.valueOf(chars); System.out.println(s1); //static String valueOf(int i) System.out.println(String.valueOf(1) + 1); System.out.println(String.valueOf(new StringDemo())); System.out.println(String.valueOf(3.3) + 1); //String toLowerCase() String s2 = "ABcdEF"; System.out.println(s2.toLowerCase()); System.out.println(s2.toUpperCase()); //String concat(String str) //字符串连接 System.out.println(s2.concat("lalala")); } }
String类的替换功能: String replace(char old,char new) //用新字符替换字符串中所有旧字符。 String replace(String old,String new) // 用新的字符串内容替换所有旧的字符串内容。 String类去除空字符串。 String trim() String类的比较功能: int compareTo(String str) int compareToIgnoreCase(String str) 接口: Comparable { //比较大小的协议 int compareTo(Object obj); } compareTo方法 返回值是 负数 代表当前对象 < 待比较对象 0 代表两个对象 == 正数 当前对象 > 待比较对象
String类方法的使用的更多相关文章
- String类方法
1.charAt(int index) 返回指定索引处的 char 值. 2. length() 返回此字符串的长度. 3.String replace(char oldChar, char new ...
- java学习之路--String类方法的应用
消除字符串两端的空格 1.判断字符串第一个位置是否为空格,如果是继续向下判断,直到不是空格位置,末尾也是这样,往前判断,直到不是空格为止. 2.当开始和末尾都不是空格时,获取字符串. public s ...
- 【实用类String】String类方法的应用案例:查找判断指定字符出现的次数和位置
一.应用要求 输入一个字符串,再输入要查找的字符,判断该字符在该字符串中出现的次数. 二.实现思路 1.使用substring()方法将字符串的每个字符存入数组 2.比较数组每个字符是否与指定的字符相 ...
- SummerVocation_Learning--java的String类方法总结
壹: public char charAt(int index),返回字符串中第index个字符. public int length(), 返回字符串长度. public int indexOf(S ...
- AJPFX关于部分String类方法
string类使用于描述字符串事物常见的操作:1.获取: 1.1 字符串中的包含的字符数,也就是字符串的长度 int length():获取字符串的长度 1.2 根据位置获取位置上的某 ...
- 原生JS:String对象详解
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- String类详解(1)
首先String是一个类. 1,实例化String类方法. 1)直接赋值:String name="haha"; 2)通过关键字:String name=new String(&q ...
- c#语言基础编程—string
引言 在c#中经常会有相关的string的操作,string类型为引用类型,集成于Object,所以会有四个方法.详情可见 值类型和引用类型的区别 里面详细介绍了,值类型和引用类型的区别和应用场合,所 ...
- java中String类型的相关知识
String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...
随机推荐
- 洛谷P1310 表达式的值 题解 栈/后缀表达式的应用
题目链接:https://www.luogu.org/problem/P1310 本题涉及算法:栈.前缀表达式转后缀表达式,动态规划思想. 这道题目我思考了好长时间,第一时间让我做的话我也做不出来. ...
- tensorflow op tf.global_variables_initializer
一.安装目前用了tensorflow.deeplearning4j两个深度学习框架, tensorflow 之前一直支持到python 3.5,目前以更新到3.6,故安装最新版体验使用. 慢慢长征路: ...
- Hibernate @OneToOne懒加载实现解决方案
在hibernate注解(三)中,我提高过一对一(@OneToOne)懒加载失效的问题.虽然给出了解决方法,但并没有给出完整的解决方案.今天我专门针对该问题进行讨论.至于懒加载失效的原因,在之前的文章 ...
- vue echarts引用
<template> <!--为echarts准备一个具备大小的容器dom--> <div id="main" style="width: ...
- P1100 三连击
题目描述 我们假设一个三位整数 \(N(100 \le N \le 999)\) ,它的百位上的数字是 \(A\) ,十位上的数字是 \(B\) ,个位上的数字是 \(C\) ,如果 \(A\) , ...
- 2018-8-10-win10-uwp-获取文件夹出错
title author date CreateTime categories win10 uwp 获取文件夹出错 lindexi 2018-08-10 19:16:50 +0800 2018-2-1 ...
- java位运算和无符号运算
计算机在底层使用的是二进制补码进行运算. 计算规则: 正数的原码.反码.补码是其二进制本身. 负数的原码首先计算其二进制数,然后最高位使用1表示负数,反码是最高位不变其它位取反,补码是在反码的基础上进 ...
- js快速替换json里的key值
需求是将b根据a的值替换对象中的key值 let a = ["code","name","date","font"]; ...
- jQuery 工具类函数-检测对象是否为空
在jQuery中,可以调用名为$.isEmptyObject的工具函数,检测一个对象的内容是否为空,如果为空,则该函数返回true,否则,返回false值,调用格式如下: $.isEmptyObjec ...
- 2019-3-1-C#-double-好用的扩展
title author date CreateTime categories C# double 好用的扩展 lindexi 2019-3-1 9:19:5 +0800 2018-05-15 10: ...