首页 / JAVA / JavaSE:String工具
JavaSE:String工具
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaSE:String工具,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4749字,纯文字阅读大概需要7分钟。
内容图文
![JavaSE:String工具](/upload/InfoBanner/zyjiaocheng/717/6503e955f6e04cc1a2bb5cd347b3ddec.jpg)
String工具
欢迎来到String工具类的笔记,把遇到的String的方法做个记录。
1.equals:
equals()方法 比较两个字符串的每个字符是否相同,如果字符都相同返回true,否则返回false ,区分大小写。
public class StringDemo {
public static void main(String[] args) {
String str1 = new String("Hello");
String str2= "Hello";
System.out.println(str1.equals(str2));
}
}
String的和equals区别:
引用类型 比较对象的引用是否指向同一个地址(Eclipse中,id是否相同) , true 两个引用指向同一个地址,String 比较字符串内容(值)是否相同,使用equals()方法,而不是==。
indexOf(str):
返回参数在当前字符串中的下标。
public class String3 {
public static void main(String[] args) {
/*public int indexOf(int ch, int fromIndex): 返回从 fromIndex
位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。*/
String str = "Hello World!!!";
int indexOf = str.indexOf("ld", 2);
//返回参数的索引(首字母)
int indexOf2 = str.indexOf("World");
//lastIndexOf()方法:返回参数在当前字符串中最后的索引
int lastIndexOf = str.lastIndexOf("!");
System.out.println(indexOf);
System.out.println(indexOf2);
System.out.println(lastIndexOf);
}
}
replace(str1,str2) :
字符串的替换
public class ReplaceText {
public static void main(String[] args) {
String str = new String("Hello World!!!");
String replace = str.replace("!!!", "~~~");
System.out.println(replace);
//2019-09-09T20:53:20去除时间中间的T。
LocalDateTime withNano = LocalDateTime.now().withNano(0);
String string = withNano.toString();
String replace2 = string.replace("T", " ");
System.out.println(replace2);
}
}
方法会重新产生一个字符串,而不是在原始字符串上进行修改
由于会重新产生一个新字符串,所以不建议在循环中使用该方法
split(str):
将当前字符串按照指定的参数进行切割,返回一个字符串数组
public class SplitTest {
public static void main(String[] args) {
String str = new String("Hello World!!!");
//按空格分割
String[] split = str.split(" ");
System.out.println(Arrays.toString(split));
}
}
subString(begin,end):
在当前字符串的基础上,取子字符串
public class SubStringText {
public static void main(String[] args) {
String str="qwer";
String substring = str.substring(0, 3);
System.out.println(substring);
}
}
concat() : 拼接字符串
//工作中可以使用+拼接字符串,但是不要在循环中使用,内存消耗过多,导致程序的阻塞,严重的会发生OutOfMemoryError。
public class ConcatText {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
String str="";
for(int i = 0;i<200000;i++){
//str+="i";
str = str.concat("1");
}
long end = System.currentTimeMillis();
System.out.println(end-begin);
}
}
trim():
去掉字符串前后的空格
public class Trim {
public static void main(String[] args) {
String str =" hello ";
String trim = str.trim();
System.out.println(trim);
}
}
toUpperCase():
小写字母转大写字母。
public class ToUpperCase {
public static void main(String[] args) {
String str ="hello";
String upperCase = str.toUpperCase();
System.out.println(upperCase);
}
}
toLowerCase:将大写转为小写:
public class ToUpperCase {
public static void main(String[] args) {
String str ="hello";
String lowerCase = str.toLowerCase:;
System.out.println(upperCase);
}
}
startsWith(str):
判断当前字符串是否已指定的参数开始
public class StartsWithTest {
public static void main(String[] args) {
String str="hello";
//测试以特定索引开始的字符串的子字符串是否以指定的前缀开始。
boolean startsWith = str.startsWith("e", 2);
//测试此字符串是否以指定的前缀开始.
boolean sw = str.startsWith("h");
System.out.println(startsWith);
System.out.println(sw);
}
}
length():
获取字符串长度
public class LengthTest {
public static void main(String[] args) {
//字符串如何转换为字符数组.
String string = "Hello";
char[] a = new char[string.length()];
for(int i = 0 ;i<a.length;i++){
char data = string.charAt(i);
a[i]=data;
}
System.out.println(Arrays.toString(a));
}
}
valueOf(char ch[]) :
该方法是一个静态方法,也是一个转换方法,它提供了多种重载方法,能够将任意类型转换为String类型 。
char[] a ={'h','e','l','l','o'};
for(int i = 0 ; i <a.length/2;i++){
char temp = a[i];
a[i]=a[a.length-i-1];
a[a.length-i-1]=temp;
}
System.out.println(Arrays.toString(a));
System.out.println(String.valueOf(a));
toCharArray();
将字符串转换为字符数组.
String name="Tomson";
//[T, o, m, s, o, n]
char ch[] = name.toCharArray();
System.out.println(Arrays.toString(ch));
String的构造方法
public class StringTestConstruct {
public static void main(String[] args) {
// String str= new String();
// String str2 = new String("Tom");
byte[] data = "ABCDEFGHIJKLMN".getBytes();
//接受一个字节数组作为参数,将字节数组转换为字符串
String str3 =new String(data);
System.out.println(str3);
//参数1:字节数组
//参数2:偏移量 此时为0,从字节数组的起始处开始拷贝
//参数3:data.length 从字节数组的起始处开始拷贝,拷贝的长度是字节数组的长度
//将直接数组所有的数据拷贝到新的字符串中
String str4 = new String(data, 0,data.length);
//ABCDEFGHIJKLMN
System.out.println(str4);
}
}
内容总结
以上是互联网集市为您收集整理的JavaSE:String工具全部内容,希望文章能够帮你解决JavaSE:String工具所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。