• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
取名字什么的最麻烦
如果套路不是为了装逼 那将毫无意义
博客园    首页    新随笔    联系   管理    订阅  订阅

String字符串常用方法


//        1、查找“asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd”该字符串中有多少个af

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {
       String asd = "asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd";
        asd.trim();
        char [] array = asd.toCharArray();
        int index = 0;
        for (int i = 0; i < array.length-2; i++) {
            if (array[i] == 'a' & array[i+1] == 'f');
                index++;
            }
        System.out.println(index);

 


//        2、输入任意一个字符串,如:“abDEe23dJfd343dPOddfe4CdD5ccv!23rr”。取出该字符串中所有的字母。顺序不能改变!
//        并把大写字母变成小写,小写字母变成大写!

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {     
        String str = "abDEe23dJfd343dPOddfe4CdD5ccv!23rr";
        str.trim();
        char [] array = str.toCharArray();
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= 'A'&& array[i] <= 'Z') {
                array[i] += 32;
            }
            else if (array[i] >='a'&& array[i] <='z') {
                array[i] = (char) (array[i] - 32);
            }
            System.out.print(array[i]);
        }

 


//        3、写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。
//        如字符串”PEN”包含在字符串“INDEPENDENT”中。

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {     

        String str2 = "wfw";
        String str1 = "adafafawawfaffawfwa";
        System.out.println(str1.contains(str2));    

 

//        4、编写程序,统计出字符串“want you to know one thing”中字母n和字母o的出现次数。

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {
        String str = "want you to know one thing";
        str.trim();
        char [] array=str.toCharArray();
        int index = 0;
        int temp = 0;
        for (int i = 0; i < array.length; i++) {
             if (array[i] == 'n') {
                index++;
            }
            else if (array[i] == 'o') {
                temp++;
            }
        }
        System.out.println("n出现"+index+"次,o出现"+temp+"次");


//        5:1)从字符串“LOVOT90班20111208”中提取开班日期

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {
        String str = "LOVOT90班20111208";
        str.trim();
        System.out.println(str.substring(8));

 

       
//        2)将“LOVO JAVA”字符串中的“JAVA”替换为“J2EE”。

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "LOVO JAVA";
        str.trim();
        System.out.println(str.replace("JAVA", "J2EE"));    

 

//        3)取出“LOVOT96班20120611”第8个字符。

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "LOVOT96班20120611";
        str.trim();
        System.out.println(str.charAt(8));

 

//        4)清除“LOVOT96班20120611”中所有的0。

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "LOVOT96班20120611";
        str.trim();
        String str1 =str.replace('0', ' ');
        String [] array = str1.split(" ");
        System.out.println(array[0] + array[1] + array[2]);

 

                
//        5)清除“LOVOT96班20120611 LOVO 老师”中所有的空格。

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "LOVOT96班20120611 LOVO 老师";
        str.trim();
        String [] array = str.split(" ");
        System.out.println(array[0]+array[1]+array[2]);

 

 

//        6)从任意给定的身份证号码中提取此人的出生日期

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String id = "511321199209067274";
        System.out.println("生日号码是:"+id.substring(6, 14));
        

 

 

//        6、查看API DOC文档。自己查找到String提供的方法完成以下功能:
//        1>、将"Hello"和"World"两个字串连接到一起。

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "Hello";
        System.out.println(str.concat("World"));
        

 

 

//        2>、将"I saw a saw saw a saw !"将里面所有的‘s’替换成‘S’

 

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "I saw a saw saw a saw !";
        str.trim();
        System.out.println(str.replace('s', 'S'));
        

 


//        3>、解析
//        "http://localhost:8080/login.jsp?username=zhang3&password=1234567",
//        将得到的用户名和密码的值放到一个String数组中。

package com.lovo.homeWork;

public class TestString {
    
    public static void main(String[] args) {

        String str = "http://localhost:8080/login.jsp?username=zhang3&    password=1234567";
        str.trim();
        String [] str1 = str.split("[?]");
        String [] str2 = str1[1].split("[&]");
        String [] str3 = str2[0].split("[=]");
        String [] str4 = str2[1].split("[=]");
        System.out.println("账号是:"+str3[1]+"密码是:"+str4[1]);
    }
}
posted @ 2016-12-12 20:33  Jrc.Engineer  阅读(491)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3