String字符串常用函数以及示例 JAVA基础


一、String类的介绍

String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。 字符串不变; 它们的值在创建后不能被更改。

二、字符串常用函数

1.int length() 获取字符串的长度
String str = "hello world!"; 声明并初始化
// 获取str的长度
System.out.println(str.length());
运行结果:
	12
2.char charAt(int index): .
  String str = "hello world!";
  // 截取下标为1的字符
System.out.println(str.charAt(1)); 返回指定索引处的char值

运行结果:
	e
3. int indexOf(int ch)
    String str = "hello world!";
    // 返回的是ch在字符串中第一次出现的索引位置
    System.out.println(str.indexOf("w"));
    运行结果:6
4. int indexOf(int ch,int fromIndex)
String str = "hello world!";
// 从formIndex指定位置开始,获取ch在字符串中出现的索引位置
    System.out.println(str.indexOf("w",7));
    运行结果:-1
5. int indexOf(String str)
String str = "hello world!";
//返回的是str在字符串中第一次出现的索引位置
        System.out.println(str.indexOf("d"));
        运行结果:10

6.lastIndexOf 返回指定字符在此字符串中最后一次出现处的索引

   String Str = new String("33923705321@qq.com");
    String SubStr1 = new String("qq");
    String SubStr2 = new String("com");
    System.out.print("查找字符 o 最后出现的位置 :" );
    System.out.println(Str.lastIndexOf( 'o' ));
    System.out.print("从第14个位置查找字符 o 最后出现的位置 :" );
    System.out.println(Str.lastIndexOf( 'o', 14 ));
    System.out.print("子字符串 SubStr1 最后出现的位置:" );
    System.out.println( Str.lastIndexOf( SubStr1 ));
    System.out.print("从第十五个位置开始搜索子字符串 SubStr1最后出现的位置 :" );
    System.out.println( Str.lastIndexOf( SubStr1, 15 ));
    System.out.print("子字符串 SubStr2 最后出现的位置 :" );
    System.out.println(Str.lastIndexOf( SubStr2 ));
    运行结果:  查找字符 o 最后出现的位置 :16
    从第14个位置查找字符 o 最后出现的位置 :-1
   子字符串 SubStr1 最后出现的位置:12
   从第十五个位置开始搜索子字符串 SubStr1最后出现的位置 :12
   子字符串 SubStr2 最后出现的位置 :15        

7.toUpperCase() 转换成大写 toLowerCase()转换成小写

8.String[] split(“字符”) 根据给定的正则表达式的匹配来拆分此字符串。形成一个新的String数组。

String str = "hello,world,你,好";
String[] strs = str.split(",");
for (String s : strs) {
    System.out.println(s);
}
运行结果:
	hello
    world
    你
    好
String str="username=? or username=? and pwd=?";
String[] str1=str.split("or|and");
    	 for (int i = 0; i < str1.length; i++) {
			  System.out.print(" "+str1[i]);结果为username=?   username=?   pwd=?
		}

9.String trim()去掉字符串左右空格

10.String replace(char oldChar,char newChar)新字符替换旧字符

String str = "hello,world,你,好";
String replace = str.replace(",","-");
System.out.println(replace);

运行结果:
	hello-world--

11.String substring(int beginIndex,int endIndex) 截取字符串,包括beginIndex位置的,不包括endIndex位置的

String str = "hello,world,你,好";
// 截取0-4位置的字符串,包括0,不包括4
String substring = str.substring(0, 4);
System.out.println(substring);

运行结果:
	hell

 String s = new String("33923705321@qq.com.ywy");
 System.out.println (    s.substring(1, s.lastIndexOf("@")));    //截取@之前的字符串
运行结果:33923705321

//        获得第一个点的位置
        int index=s .indexOf(".");
        index = s .indexOf(".", index + 1);

        //根据第二个点的位置,截取 字符串。得到结果 result
        String result=s .substring(index+1);
        System.out.println(result);//输出第二个点位置的字符
运行结果:ywy  

  1. boolean equalsIgnoreCase(String str2)
    忽略大小写的比较两个字符串的值是否一模一样,返回一个布尔值
    bolean equals(String str2) 比较两个字符串的值是否一模一样
String str = "hello world!";
String str2 = "HELLO WORLD!";
System.out.println(str.equalsIgnoreCase(str2));
System.out.println(str.equals(str2));

运行结果:
	true
	false

13.boolean contains(String str2) 判断一个字符串里面是否包含指定的内容,返回一个布尔值

String str = "hello world!";
System.out.println(str.contains("hel"));

运行结果:
	true
  1. boolean startsWith(String str)测试此字符串是否以指定的前缀开始。返回一个布尔值
    boolean endsWith(String str)测试此字符串是否以指定的后缀结束。返回一个布尔值
  2. String replaceAll(String str1,Stringstr2)将某个内容全部替换成指定内容
String str = "hello world!";
String replaceAll = str.replaceAll("l", "m");
System.out.println(replaceAll);

运行结果:
	hemmo wormd!

16.String replaceFirst(String str1,String str2) 将第一次出现的某个内容替换成指定的内容
String str = “hello world!”;
String replaceAll = str.replaceFirst(“l”, “m”);
System.out.println(replaceAll);

运行结果:
hemlo world!

17.字符串反转

   public static void fun(String string){
        char[] array = string.toCharArray();
         for(int i = 0; i < array.length/2; i++){
            char temp = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - i -1] = temp;
        }

        String str = String.valueOf(array);
        System.out.println(str); //传入abcde 输入edcba
    }

18.统计字符出现次数

  // 定义字符串
    String string = "fdafasfsfasf";

    // 定义map容器
    Map<Character, Integer> map = new HashMap<Character, Integer>();
    for (int i = 1; i < string.length(); i++) {

        char ch = string.charAt(i);

        if (map.containsKey(ch)) {
            int count = map.get(ch);
            count = count + 1;
            map.put(ch, count);
        } else {
            map.put(ch, 1);
        }
    }
    // 遍历map集合
    Set<Character> keySet = map.keySet();
    for (Character chars : keySet) {
        System.out.println("字符:" + chars + ",出现的次数为:" + map.get(chars));
    }

}
运行结果 :字符:a,出现的次数为:3  字符:s,出现的次数为:3 字符:d,出现的次数为:1 字符:f,出现的次数为:4


  //获取一个字符串,查找这个字符串出现的次数;  输入abcabcacb ,"a"   输出3
 public static int getStringCount(String str, String key) {
int count = 0;
int index = 0;
int num = str.indexOf(key);
while ((index = str.indexOf(key)) != -1) {
    count++;
    str = str.substring(str.indexOf(key) + key.length());
}
return count;
  }

18.将一个数组从左开始第几位之前的进行旋转:左旋数组
如:将"abcdef"第2位之前(a为0号位置)进行旋转----》“cdefab”。

public class TextDemo4 {
public static void leftRotateString(String str, int n) {
    char[]array1 = str.toCharArray();//将给定字符串转换成数组
    char[]array2 = new char[array1.length];//定义新数组存储旋转后的元素
    int j = 0;//i表示源数组的下标,j表示新数组的下标
    for (int i = n; i <array1.length ; i++) {
        array2[j] = array1[i];
        j++;
    }
    for (int i = 0; i <n ; i++) {
        array2[j] =array1[i];
        j++;
    }
    String str1 = new String(array2);
    System.out.println(str1);
}
public static void main(String[] args) {
    leftRotateString("abcdef",2);
 }
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咕噜咕噜wy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值