欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java自学-数字与字符串 操纵字符串

程序员文章站 2023-10-27 08:57:46
Java常见字符串方法 示例 1 : 获取字符 charAt(int index)获取指定位置的字符 package character; public class TestString { public static void main(String[] args) { String senten ......

java常见字符串方法

示例 1 : 获取字符

charat(int index)获取指定位置的字符

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
         
        char c = sentence.charat(0);
         
        system.out.println(c);
           
    }
}

示例 2 : 获取对应的字符数组

tochararray()
获取对应的字符数组

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
 
        char[] cs = sentence.tochararray(); //获取对应的字符数组
         
        system.out.println(sentence.length() == cs.length);
         
    }
}

示例 3 : 截取子字符串

substring
截取子字符串

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
         
        //截取从第3个开始的字符串 (基0)
        string substring1 = sentence.substring(3);
         
        system.out.println(substring1);
         
        //截取从第3个开始的字符串 (基0)
        //到5-1的位置的字符串
        //左闭右开
        string substring2 = sentence.substring(3,5);
         
        system.out.println(substring2);
         
    }
}

示例 4 : 分隔

split
根据分隔符进行分隔

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
         
        //根据,进行分割,得到3个子字符串
        string subsentences[] = sentence.split(",");
        for (string sub : subsentences) {
            system.out.println(sub);
        }
           
    }
}

示例 5 : 去掉首尾空格

trim
去掉首尾空格

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "        盖伦,在进行了连续8次击杀后,获得了 超神 的称号      ";
         
        system.out.println(sentence);
        //去掉首尾空格
        system.out.println(sentence.trim());
    }
}

示例 6 : 大小写

tolowercase 全部变成小写
touppercase 全部变成大写

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "garen";
         
        //全部变成小写
        system.out.println(sentence.tolowercase());
        //全部变成大写
        system.out.println(sentence.touppercase());
         
    }
}

示例 7 : 定位

indexof 判断字符或者子字符串出现的位置
contains 是否包含子字符串

package character;
     
public class teststring {
     
    public static void main(string[] args) {
    
        string sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
  
        system.out.println(sentence.indexof('8')); //字符第一次出现的位置
          
        system.out.println(sentence.indexof("超神")); //字符串第一次出现的位置
          
        system.out.println(sentence.lastindexof("了")); //字符串最后出现的位置
          
        system.out.println(sentence.indexof(',',5)); //从位置5开始,出现的第一次,的位置
          
        system.out.println(sentence.contains("击杀")); //是否包含字符串"击杀"
          
    }
}

示例 8 : 替换

replaceall 替换所有的
replacefirst 只替换第一个

package character;
    
public class teststring {
    
    public static void main(string[] args) {
   
        string sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
 
        string temp = sentence.replaceall("击杀", "被击杀"); //替换所有的
         
        temp = temp.replaceall("超神", "超鬼");
         
        system.out.println(temp);
         
        temp = sentence.replacefirst(",","");//只替换第一个
         
        system.out.println(temp);
         
    }
}

练习

把 lengendary 改成间隔大写小写模式,即 lengendary

答案

package character;
 
public class teststring {
      
    public static void main(string[] args) {
//      把 lengendary 改成间隔大写小写模式,即 lengendary 
        string s = "lengendary";
        char[] cs =s.tochararray();
        int count= 0;
        for (int i = 0; i < cs.length; i++) {
            if(0==i%2)
                cs[i] = character.touppercase(cs[i]);
        }
        string result = new string(cs);
        system.out.printf(result);
 
    }
}

练习

nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak
把最后一个two单词首字母大写

答案

package character;
 
public class teststring {
 
    public static void main(string[] args) {
        // 把最后一个two单词首字母大写
        string s = "nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
        int index = s.lastindexof(" two ");
         
        char[] cs = s.tochararray();
        cs[index +1] = character.touppercase(cs[index+1]);
        string result = new string(cs);
        system.out.printf(result);
 
    }
}