Java中String类的常用方法
判断功能的方法
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase
(String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
public class String_Demo01 {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO"; // boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
System.out.println("-----------"); //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("-----------");
}
}
Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中
注意:
2个字符串使用==比较运算符,比较的是地址值,如果使用的是equals方法,比较的是字符串内容是否相等
获取功能的方法
public int length () :返回此字符串的长度。
String s = "helloworld";
//int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());//
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
String s = "helloworld";
//String concat:将指定的字符串连接到该字符串的末尾
String s2 = s.concat("**hello itheima");
System.out.println(s2);//helloworld**hello itheima
public char charAt (int index) :返回指定索引处的 char值。
String s = "helloworld";
//char charAt:获取指定索引处的字符
System.out.println(s.charAt(0));//h
System.out.println(s.charAt(1));//e
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
String s = "helloworld";
// 获取子字符串第一次出现在该字符串内的索引,没有返回-1
System.out.println(s.indexOf("l"));//
System.out.println(s.indexOf("wow"));//-1
System.out.println(s.indexOf("ak"));//-1
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
String s = "helloworld";
// 从beginIndex开始截取字符串到字符串结尾
System.out.println(s.substring(0));//helloworld
System.out.println(s.substring(5));//world
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
String s = "helloworld";
// 从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
System.out.println(s.substring(0, s.length()));//helloworld
System.out.println(s.substring(3,8));//lowor
转换功能的方法
public char[] toCharArray () :将此字符串转换为新的字符数组。
String s = "HelloWorld!";
//char[] toCharArray:把字符串转换为字符数组
char[] chs = s.toCharArray();
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
String s = "HelloWorld!";
byte[] bytes = s.getBytes();
public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
String str = "itcast itheima";
String replace = str.replace("it","IT");
分割功能的方法
有些特殊符号需要用 反斜杠 \ 转义,在Java要用两个反斜杠 \\
public String[] split(String regex)
:将此字符串按照给定的regex(规则)拆分为字符串数组。
//String分割
String s = "aa|bb|cc";
String[] strArray = s.split("\\|"); for(int i = 0; i < strArray.length; i++){
System.out.print(strArray[i]);
}
一些常用方法
boolean contains(CharSequence s): 判断字符串中是否包含指定字符。
String s = "djlfdjksdlka";
boolean str = s.contains("g");
System.out.println("str" + str);
Java中String类的常用方法的更多相关文章
- java 中String类的常用方法总结,带你玩转String类。
String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...
- java 中String类的常用方法总结,玩转String类
String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...
- Java中String类的方法及说明
String : 字符串类型 一. String sc_sub = new String(c,3,2); // String sb_copy = new String(sb) ...
- 【转载】Java中String类的方法及说明
转载自:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html String : 字符串类型 一. String sc_ ...
- java中String类学习
java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...
- 在java中String类为什么要设计成final
在java中String类为什么要设计成final? - 胖胖的回答 - 知乎 https://www.zhihu.com/question/31345592/answer/114126087
- java中File类的常用方法总结
java中File类的常用方法 创建: createNewFile()在指定的路径创建一个空文件,成功返回true,如果已经存在就不创建,然后返回false. mkdir() 在指定的位置创建一个此抽 ...
- 在java中String类为什么要设计成final?
大神链接:在java中String类为什么要设计成final? - 程序员 - 知乎 我进行了重新排版,并且更换了其中的一个例子,让我们更好理解. String很多实用的特性,比如说“不可变性”,是工 ...
- Java中String类为什么被设计为final?
Java中String类为什么被设计为final 首先,String是引用类型,也就是每个字符串都是一个String实例.通过源码可以看到String底层维护了一个byte数组:private f ...
随机推荐
- [luoguP2024] 食物链(并查集)
传送门 经典的并查集问题 对于这种问题,并查集需要分类 开3*n的并查集,其中x用来连接与x同类的,x+n用来连接x吃的,x+2*n用来连接x被吃的. 1 x y时,如果 x吃y 或 x被y吃,那么为 ...
- 清北学堂模拟赛d6t5 侦探游戏
分析:简化一下题意就是给任意两对点连一条权值为0的边,求出每次连边后最小生成树的权值和*2/(n - 1) * n. 每次求最小生成树肯定会爆炸,其实每次加边只是会对最小生成树上的一条边有影响,也就是 ...
- [bzoj3339]Rmq Problem||[bzoj3585]mex_线段树
Rmq Problem bzoj-3339||mex bzoj-3585 题目大意:给定一个长度为n的数列a,多次讯问区间l,r中最小的不属于集合{$A_l,A_{l+1}...A_r$}的非负整数. ...
- sqlServer杂计
In与Exists的区别 这两个函数是差不多的,但由于优化方案不同,通常NOT Exists要比NOT IN要快,因为NOT EXISTS可以使用结合算法二NOT IN就不行了,而EXISTS则不如I ...
- IntelliJ IDEA 在左右两侧出现Project、Maven Project等导航按钮
IntelliJ IDEA 在左右两侧出现Project.Maven Project等导航按钮 选中 View > Tool Buttons 可以查看Project.Maven Project等 ...
- pytest 失败用例重试
https://www.cnblogs.com/jinzhuduoduo/articles/7017405.html http://www.lxway.com/445949491.htm https: ...
- ubuntu 关机命令
ubuntu 关机命令 关机命令 shutdown ubuntu的终端中默认的是当前用户的命令,只是普通用户,因此在终端器中可以使用sudo -sh 转换到管理员root用户下执行命令. 1)shut ...
- 将TensorFlow模型变为pb——官方本身提供API,直接调用即可
TensorFlow: How to freeze a model and serve it with a python API 参考:https://blog.metaflow.fr/tensorf ...
- KD树——k=1时就是BST,里面的数学原理还是有不明白的地方,为啥方差划分?
Kd-Tree,即K-dimensional tree,是一棵二叉树,树中存储的是一些K维数据.在一个K维数据集合上构建一棵Kd-Tree代表了对该K维数据集合构成的K维空间的一个划分,即树中的每个结 ...
- B1295 [SCOI2009]最长距离 最短路
就是一道最短路的裸题,直接跑spfa就行了.(spfa死了) 最后在答案处判断是否障碍物太多,然后就直接找最大值就行. (数据特别水,我错误算法60) 题干: Description windy有一块 ...