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()将这个字符串中 ...
随机推荐
- python深浅copy和赋值
Python直接赋值,浅copy和深copy的比较 基于引用和对象(python引用和对象分离) 总结: 直接赋值:a = b -->a,b两个引用指向相同的对象 浅copy:a为b的copy ...
- (二)Centos7下Yum更新安装PHP5.5,5.6,7.0
yum源默认的版本太低了,手动安装有一些麻烦,想采用Yum更新安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包, ...
- [转][Linux/Ubuntu] vi/vim 使用方法讲解
vi/vim 基本使用方法 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的 ...
- vue 使用webpack打包后路径报错以及 alias 的使用
一.vue 使用webpack打包后路径报错(两步解决) 1. config文件夹 ==> index.js ==> 把assetsPublicPath的 '/ '改为 './' 2. b ...
- 2019-8-31-win10-uwp-使用-WinDbg-调试
title author date CreateTime categories win10 uwp 使用 WinDbg 调试 lindexi 2019-08-31 10:30:35 +0800 201 ...
- css隐藏滚动条、移动端滚动卡顿的解决
1.如果想保持容器能够滚动,同时不想看到丑陋的滚动条,chrome.firefox和移动端上不考虑兼容性直接 element::-webkit-scrollbar{ display:none } 2. ...
- Java 学习笔记(15)——反射
Java中的类文件最终会被编译为.class 文件,也就是Java字节码.这个字节码中会存储Java 类的相关信息.在JVM执行这些代码时首先根据 java 命令中指定的类名找到.class 文件然后 ...
- Cisco DNA-C POC环境配置
Step1:在DNA-C上创建Site,本例创建Global->China->WangJiang->20 F如下图: Step2:配置fusion区域的AAA和NTP等信息,如下图: ...
- FreeNOS学习2——操作系统是如何启动的
The System Boot Process Explained:https://www.webopedia.com/DidYouKnow/Hardware_Software/BootProcess ...
- 【原创】http请求中加号被替换为空格?源码背后的秘密
这是why技术的第**20**篇原创文章![在这里插入图片描述](https://user-gold-cdn.xitu.io/2019/12/30/16f550eb82e10eff?w=900& ...