zoukankan      html  css  js  c++  java
  • java之练习字符串的处理

    public class Lianxi{
    
      public static void main(String[] args){
    
        String str="像勇士这样的球队,只有防守一松懈,他们才能抓住机会,打完了三场,爵士还是没找到应对勇士的办法";
    
    
                //1.写代码找出关键字“球队”,“机会”所在字符串str的索引位置,找出字符串中第二个“勇士”的位置,并输出在控制台上
                int i1=str.indexof("球队");
                int i2=str.indexof("机会");
                int i3=str.lastIndexof("勇士");
                Syetem.out.println(i1);
                Syetem.out.println(i2);
                Syetem.out.println(i3);
    
    
                //2.定义int型变量m,取值为第一题中所有索引值的和
                int m=i1 +i2 +i3;    
    
    
                //3.在控制台上输出m作为char型时显示的内容
                System.out.println((char)m);
    
    
    
                //4.写代码实现将str字符串用","分割成数组, 并输出索引值为4的值
                System.out.println(str.split(",")[4]);
    
    
    
                //5.写代码实现将str字符串中"爵士"用"勇士"代替, "勇士"用"爵士"代替, 并将结果输出到控制台上(禁用replace方法)
                String[] newstr=str.split("");
                String temp ="";
                for(int i=0;i<newstr.length;i++){
                    if("爵".equals(newstr[i])){
                        newstr[i] ="勇";
                    }else if("勇".equals(newstr[i])){
                        newstr[i] ="爵";
                    }
                    temp +=newstr[i];
                }
                System.out.println(temp);
    
    
    
                //6.编写代码从str字符串中取一部分在控制台上打印这样一句话: 勇士抓住机会,找到应对办法
                int a=str.indexof("勇士");
                System.out.print(str.substring(a, a+2));
                int b=str.indexof("抓住机会");
                System.out.print(str.substring(b, b+4));
                int c=str.indexof("找到应对");
                System.out.print(str.substring(c, c+4));
                int d=str.indexof("办法");
                System.out.print(str.substring(d, d+2));
    
    
    
    
    
                //7.写一段代码, 可以取出任意qq邮箱地址中的qq号码
                String qqemail = "12345678@qq.com";
                System.out.println(qqemail.substring(0, qqemail.indexof("@")));
                
    
                String[] a= qqemail.split("@");
                System.out.println(a[0]);
     
    
         
      }
    }
    
                                                                                                                             
     1 public class Lianxi{
     2 
     3   public static void main(String[] args){
     4 
     5        //1.使用for和if打印一个空心正方形
     6         int n= 5;
     7         for(int i=0; i<n; i++){
     8             for(int j=0; j<n; j++){
     9                 if(i==0 || i==4){
    10                     System.out.print("* ");
    11                 }else{
    12                     if(j==0 || j==4){
    13                         System.out.print("* ");
    14                     }else{
    15                         System.out.print(" ");
    16                     }
    17                 }   
    18             }
    19             System.out.println();
    20         }
    21 
    22 
    23 
    24        //2.使用for循环打印一个菱形
    25                 int n=4;
    26         for(int i=0; i<n; i++){
    27             for(int j=0; j<n-i-1; j++){
    28                 System.out.print(" ");
    29             }
    30             for(int k=0; k<2*i+1; k++){
    31                 System.out.print("*");
    32             }
    33             System.out.println();
    34         }
    35         for(int a=0; a<n-1;a++){
    36             for(int b=0; b<a+1; b++){
    37                 System.out.print(" ");
    38             }
    39             for(int c=0; c<5-(2*a); c++){
    40                 System.out.print("*");
    41             }
    42             System.out.println();
    43         }
    44 
    45   
    46   }
    47 }
  • 相关阅读:
    select()函数用法二
    fcntl函数的用法总结
    O_NONBLOCK与O_NDELAY有何不同?
    struct termios结构体详解
    select()函数用法一
    linux下的struct sigaction
    sigaction 用法实例
    nand flash 的oob 及坏块管理
    wifi两种工作模式
    UBIFS文件系统简介 与 利用mkfs.ubifs和ubinize两个工具制作UBI镜像 (完整理解版本)
  • 原文地址:https://www.cnblogs.com/zym0m/p/7204819.html
Copyright © 2011-2022 走看看