java字符串练习1

  1. 字符串解析,现有一字符串,"卡巴斯基#杀毒软件#免费版#俄罗斯#",解析出每个元素。
  2. "那车水马龙的人世间,那样地来 那样地去,太匆忙"最后一次出现""的位置。
  3. 判断输入的字符串是否是 .java 结束 

    提示:public boolean endsWith(String suffix) (查帮助,查此方法的说明) 

4.有一身份证号,判断此为男还是女,基于此方法,写一个算法,判断一个身份证号为男还是女。(身份证分15位和18位)

5.有如下格式的字符串name-age-sex-address,解析出姓名,年龄等信息。

6.求出字符串中有多少种字符,以及每个字符的个数

    static void printCharInfo(String str)

 例如有字符串 str="apple is a apple.";

   结果应该是

        a:3

        p:4

        l:2

        e:2

         :3

        i:1

        s:1

        .:1

7.定义一个方法,用来去掉字符串右边的空格

    String rtrim(String str)

   

8.定义一个方法,将str所指字符串的正序和反序进行连接,例如 "ok"->"okko"

    String concat(String str);

 

9.字符串右移n,例如   "hello world" 右移两位 ldhello wor

    要求写一个方法实现此功能,方法的格式是

    String moveToRight(String str,int position)

    str:需要移动的字符串

    p:右移的位数

   

10.求5个字符串中最长的那个,把最长的字符串打印出来

11.若可以从一个源字符串中, 找到一个相同的字符串(忽略大小写) 则返回第一个字符的索引位置,否则返回-1

int stringAt(String str,String subStr)

12.判断一个字符串是否是回文数

13.如下字符串,01#张三#20-02#李四#30-03#王五#40。。。。。。,解析每个人分数多少。样式如下:

01 张三 20

02 李四 30

03 王五 40。并且计算总分。


1.

package 字符串;

public class class01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="卡巴斯基#杀毒软件#免费版#俄罗斯#";
        String name[]=s.split("#");
        for(String i:name)
        {
            System.out.println(i);
        }
    }

}

2.

package 字符串;

public class class02 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="那车水马龙的人世间,那样地来 那样地去,太匆忙";
        System.out.println(s.lastIndexOf("那")); 
    }

    

}

3.

package 字符串;

public class class03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="liyifeng.java";
        System.out.println(s.endsWith(".java"));
    }

}
 

4.

package 字符串;

public class class04 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="123456789123456779";
        String s1=s.charAt(16)+"";
        int a=Integer.parseInt(s1, 10);
        if(a%2==0)
        {
            System.out.println("我是一个妹子!!!");
        }else
        {
            System.out.println("我是一个帅哥!!!");
        }
    }

}

5.

package 字符串;

public class class05 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="李易峰-22-男-西安";
        String [] a= {"姓名","年龄","性别","地址"};
        String name[]=s.split("-");
        for(int i=0;i<name.length;i++) {
            System.out.println(a[i]+":"+name[i]);
        }
    
                
    }

}

6.

package 字符串;

public class class06 {
    static void printCharInfo(String str)
    {
        while(str.length()>0)
        {
            int h=str.length();
        String  a=    str.charAt(0)+"";
        str=str.replace(a,"");
        System.out.println(a+":"+(h-str.length()));
        
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="apple is a apple.";
        class06.printCharInfo(s);
    }

}
 

7.

package 字符串;

public class class07 {
    static String rtrim(String str)
    {    char[] name=str.toCharArray();
    
        int l=str.length();
        System.out.println(l);
        for(int i=name.length-1;i>0;i--)
        {
            if(name[i]==' ')
            {
                l--;
            }
        }
        System.out.println(l);
        for(int i=0;i<l;i++)
            System.out.print(name[i]);
        return "";
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="13fsggs    ";
        class07 .rtrim(s);
    }

}

8.

package 字符串;

public class class08 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="ok";
        System.out.println(s+new StringBuffer(s).reverse());
        
    }

}

9.

package 字符串;

public class class09 {
public static String moveToRight(String str,int position)
{
    String s1=str.substring(str.length()- position,str.length());
    String s2=str.substring(0,str.length()-position);
    return s1+s2;
}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="hellow word";
        String s1=class09.moveToRight(s, 2);
        System.out.println(s1);
        
    }

}

10.

package 字符串;

public class class10 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] s=new String[5];
        s[0]="1234";
        s[1]="13246789";
        s[2]="123456";
        s[3]="789";
        s[4]="4567891033";
        int max=0;
        for(int i=0;i<5;i++)
        {
            if(s[i].length()>s[max].length())
                max=i;
            
        }
        System.out.println(s[max]);
    }

}

11.

package 字符串;

public class class11 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="1888";
        String s1="asdf1258666";
        s.toUpperCase();
        s1.toUpperCase();
        if(s1.contains(s))
        {
            System.out.println("yeyeye");
        }
        else
        {
        System.out.println("nonono");
        }
    }

    

}

12.

package 字符串;

public class class12 {
    public static boolean isPalindromeByBuffer(String s) {

      

        //将strOrigin作为输入参数,构造一个StringBuffer对象

        StringBuffer strBuf = new StringBuffer(s);

        //调用StringBuffer对象自带的reverse()方法进行字符串翻转,最后调用toString()返回一个String类型字符串

        String strAfterReverse = strBuf.reverse().toString();  

        //通过equals()方法判断原来的字符串和翻转后的字符串是否相等,来确定是否为回文

        return s.equals(strAfterReverse);

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String w="12345654321";
        //System.out.println(isPalindromeByBuffer(w));
        StringBuffer s=new StringBuffer(w);
        
        String s1=s.reverse().toString();
     
        if(w.equals(s1))
        {
            System.out.println("yes!!!");
        }else
        {
            System.out.println("nonono");
        }
         
        
    }

}

13.

package 字符串;

import java.util.Arrays;

public class class13 {

    public static void main(String[] args) {
        
        // TODO Auto-generated method stub
        
        String s = "01#张三#20-02#李四#30-03#王五#40";
        
        String[] name = s.split("-");
        
        for (int i = 0; i < name.length; i++) {
            
            System.out.println(name[i].replace("#", " "));
            
        }
        int a[] = new int[3];
        
        for (int i = 0; i < name.length; i++)
            
            a[i] = Integer.parseInt(name[i].substring(name[i].length() - 2, name[i].length()));
        
        // System.out.println(name[i].substring(name[i].length()-2, name[i].length()));
        
        int sum = 0;
        
        for (int i = 0; i < a.length; i++)
            
            sum += a[i];
        
        System.out.println("总分为:" + sum);
        
        
        // for(String s1:name) {
        
        // System.out.println(s1);}
        
        // }

    }
}
 

 

 

 

 

 

 

 

 

 

  • 6
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值