码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-05 16:38:15      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:sts   printf   ati   首字母   单词   test   index   code   html   

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);
 
    }
}

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

标签:sts   printf   ati   首字母   单词   test   index   code   html   

原文地址:https://www.cnblogs.com/jeddzd/p/11624702.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!