JAVA String常用方法超详细讲解
一 、常见String类的获取功能
(1) length:获取字符串长度;
String test ="abcdefg";
System.out.println("长度为:"+ test.length() );
长度为: 7
(2) charAt(int index):获取指定索引位置的字符;
String test ="abcdefg";
System.out.println( "索引为0既第一个字符为:" + test.charAt(0) );
索引为0既第一个字符为: a
(3) indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引;(数字是ASCII码中对应的字符数值)
String test ="abcdefg";
System.out.println( test.indexOf(97) );
System.out.println( test.indexOf("b") );
0
1
(4) substring(int start):从指定位置开始截取字符串,默认到末尾;
String test ="abcdefg";
System.out.println( test.substring(2) );
cdefg
(5) substring(int start,int end):从指定位置开始到指定位置结束截取字符串;
String test ="abcdefg";
System.out.println( test.substring(2,5));
cde
二、常见String类的判断功能
(1)equals(Object obj): 比较字符串的内容是否相同,区分大小写;
String test ="abcdefg";
String test1 ="abcdefg";
System.out.println( test.equals(test1) );
true
(2)contains(String str): 判断字符串中是否包含传递进来的字符串;
String test ="abcdefg";
System.out.println( test.contains("ab") );
true
(3)startsWith(String str): 判断字符串是否以传递进来的字符串开头;
String test ="abcdefg";
System.out.println( test.startsWith("ab") );
true
(4)endsWith(String str): 判断字符串是否以传递进来的字符串结尾;
String test ="abcdefg";
System.out.println( test.endsWith(fg));
true
(5)isEmpty(): 判断字符串的内容是否为空串"";
String test ="abcdefg";
System.out.println( test.isEmpty());
false
三、常见String类的转换功能
(1)byte[] getBytes(): 把字符串转换为字节数组;
String test ="abcdefg";
byte[] test1 =test.getBytes();
System.out.println( test1[0] );
97
(2)char[] toCharArray(): 把字符串转换为字符数组;
String test ="abcdefg";
char[] test1=test.toCharArray();
System.out.println( test1[0] );
a
(3)String valueOf(char[] chs): 把字符数组转成字符串。valueOf可以将任意类型转为字符串;
char[] test ={'a','b','c','d'};
String test1=String.valueOf(test);
System.out.println(test1);
abcd
(4)toLowerCase(): 把字符串转成小写;
toUpperCase(): 把字符串转成大写;
String test ="abcdefg";
String test1 ="ABCDEFG";
System.out.println(test1.toLowerCase());
System.out.println(test.toUpperCase());
abcdefg
ABCDEFG
(5)concat(String str): 把字符串拼接;
String test ="abcdefg";
String test1 ="higk";
System.out.println(test.concat(test1));
abcdefghigk
四、常见String类的其他常用功能
(1)replace(char old,char new) 将指定字符进行互换
String test ="abcdefg";
System.out.println(test.replace("a","A"));
Abcdefg
(2)replace(String old,String new) 将指定字符串进行互换
String test ="abcdefg";
System.out.println(test.replace("ab","AB"));
ABcdefg
(3)trim() 去除两端空格
String test =" abcdefg ";
System.out.println(test );
System.out.println(test.trim());
&abcdefg&(代表空格)
abcdefg
(4)int compareTo(String str)
会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字 母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果连个字符串一摸一样 返回的就是0。
String test ="abcdefg";
String test1 ="abcdefg";
String test2 ="abcdefgh";
System.out.println(test.compareTo(test1) );
System.out.println(test.compareTo(test2) );
0
-1
字符串练习:一个子串在字符串中出现的次数
思路:
1.用indexOf方法获取子串在字符串中第一次出现的位置index
2.再用indexOf方法获取以(index+子串长度)为起始的剩下的字符串中子串出现的位置,直到字符串中不再包含子串。可用while循环实现。
3.每次找到后用计数器记录即可。
public class StringTest_2
{
public static void main(String[] args)
{
String str="abcqwabcedcxabcuabcjkabcnmbabc";
//String str=null;
try
{
int count=countChildStr(str,"abc");
System.out.println("abc在"+str+"中出现的次数为:"+count);
}
catch (NullPointerException ne)
{
System.out.println(ne);
}
catch(RuntimeException re)
{
System.out.println(re);
}
}
public static int countChildStr(String str,String key)
{
if(str==null||key==null)
{
throw new NullPointerException("空指针异常,源字符串和子串都不能为NULL");
}
if(key=="")
{throw new RuntimeException("调用不合法,子串要有内容");}
int count=0,index=0;
while((index=str.indexOf(key,index))!=-1)
{
count++;
index+=key.length();
}
return count;
}
}
总结
到此这篇关于JAVA String常用方法的文章就介绍到这了,更多相关JAVA String常用方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot中application.properties与application.yml区别小结
本文主要介绍了SpringBoot中application.properties与application.yml区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-10-10
解决IDEA中多模块下Mybatis逆向工程不生成相应文件的情况
这篇文章主要介绍了解决IDEA中多模块下Mybatis逆向工程不生成相应文件的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-01-01
mybatis-plus自带保存接口,主键Id不是从1开始问题及解决
文章描述了在使用MyBatis-Plus的save方法保存数据时,遇到主键ID过大导致报错的问题,提出的解决方案包括使用@TableId注解让MyBatis-Plus使用数据库的自增模式,使用truncatetable方法重置表并备份数据,以及通过建表语句设置主键ID的初始值2025-12-12
springBoot集成Elasticsearch 报错 Health check failed的解决
这篇文章主要介绍了springBoot集成Elasticsearch 报错 Health check failed的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-08-08
IDEA POM文件配置profile实现不同环境切换的方法步骤
这篇文章主要介绍了IDEA POM文件配置profile实现不同环境切换的方法步骤2024-03-03


最新评论