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()将这个字符串中 ...
随机推荐
- SpringBoot 集成 Activiti 一路踩得坑
由于项目需要,本人开始在项目Spring boot 中集成工作流引擎Activiti.由于第一次集成,一路上步步都是坑,怪我没有先去看官方文档.现将一路上遇到的问题一一记录. 一. 环境配置 1.项目 ...
- 智课雅思词汇---九、mon是什么意思
智课雅思词汇---九.mon是什么意思 一.总结 一句话总结:词根:mon(min) = to warn, to advise, to remind 1.mit是什么意思? 词根:-mitt-, -m ...
- MySQL高级配置
参考文章:http://www.jb51.net/article/47419.htm https://blog.csdn.net/waneto2008/article/details/52502208 ...
- linux清理函数
每个非试验性的模块也要求有一个清理函数, 它注销接口, 在模块被去除之前返回所有资 源给系统. 这个函数定义为: static void exit cleanup_function(void) { ...
- 【git】Git回退代码到指定版本
1. 查看所有的历史版本,获取你git的某个历史版本的id, git log2. 回退本地代码库:git reset --hard ID3. 推送到远程服务器:git push -f -u origi ...
- Java内存溢出java.lang.OutOfMemoryError: PermGen space
今天把以前的一个项目部署在tomcat,启动没问题.因为用到了webservice,当调用webservice中的方法时一直报内存溢出异常 Exception in thread "http ...
- ios设备iframe无法滚动
在使用IFRAME,你需要使用一个元素(如DIV)来包装他们 <div class="scroll-wrapper"> <iframe src="&qu ...
- 如何抢占云栖大会C位?史上最强强强攻略来了
本文作者:阿里云头条 原文链接 本文为云栖社区原创内容,未经允许不得转载.
- Python10_代码规范和可读性
养成好的编程习惯和方法对提升代码可读性至关重要. 1.类.模块.包:不要用下划线,命名要简短 2.类:命名最好以大写开头 3.模块.包:用小写单词 4.变量.函数.方法:可以用下划线提高可读性,尽量都 ...
- HDU4528 小明捉迷藏 [搜索-BFS]
一.题意 小明S在迷宫n*m中找大明D和二明E,障碍物X不能走,问你计算是否能在时间t内找到大明和二明 二.分析 2.1与普通的BFS不同,这里可以走回头路,这里应该建立四维的标记数组标记数组,例如v ...