20180823 String练习题

package com.jn.xx;

import java.util.Scanner;

import org.junit.Test;

public class Test6 {
//junit 单元测试
@Test
public void test1() {
//根据文件名判断是否是 视频(avi mp4 mp4.jpg)
String filename = “cc.avi”;
if(filename.endsWith(“.avi”)||filename.endsWith(“.mp4”)) {
System.out.println(“是视频”);
}else {
System.out.println(“不是视频”);
}
}

/**
 * 判断.java文件名是否正确,判断邮箱格式是否正确
 * @ . 有   @在.之前
 */
@Test     //加这个注解是告诉  是一个junit测试方法
public void test2() {
    Scanner scan = new Scanner(System.in);
    String email = scan.next();
    if(email.contains("@")&&email.contains(".")) {
        int i1 =   email.indexOf("@");
        int i2 = email.indexOf(".");
        if(i1

}
@Test
public void test3() {
String a=” ab c “;
System.out.println(a);
System.out.println(a.trim());// trim 去除左右空格
}

@Test
public void test4() {
    String a="床前明月光, 疑是地上霜,举头望明月, 低头思故乡";
    String[] strarray= a.split(","); //.split("根据什么分")
    for(int i=0;i=65&&c1<=90) {
                cc++;
          }
    }
    System.out.println(cc);
}
/**
 * 床前明月光, 疑是地上霜,举头望明月, 低头思故乡
    明月  查询明月出现几次
 */
@Test
public void test8() 
{
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入一段内容");
    String str = scan.next();
    System.out.println("请输入一个子字符串");
    String son = scan.next();
    int count =0;
  while(str.indexOf(son)!=-1) {
         count++;
         str = str.substring(str.indexOf(son)+1);
  }
    System.out.println(count);
}
//StringBuffer   自带buffer
@Test
public void test9() 
{
    StringBuffer  strb = new StringBuffer("aaa");//空对象
    strb.append(",");
    strb.append("bbbb");
    System.out.println(strb.indexOf("a"));
    //stringbuffer zhuan string


}
//根据下标删除
@Test
public void test10() 
{
    StringBuffer  strb = new StringBuffer("上课的时候无精打采,到了大街上兴高采烈");//空对象
    StringBuffer str1 = strb.deleteCharAt(2);
    System.out.println(str1);
}

@Test
public void test11() 
{
    StringBuffer  strb = new StringBuffer("上课的时候无精打采,到了大街上兴高采烈");//空对象
    StringBuffer str1 = strb.delete(2,5); //钱包后不包
    System.out.println(str1);
}


@Test
public void test12() 
{
    StringBuffer  strb = new StringBuffer("上课的时候无精打采,到了大街上兴高采烈");//空对象
    StringBuffer str1 = strb.insert(2, "*");//在第A个位置 加B
    System.out.println(str1);
}
//编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕

@Test
public void test13() 
{
    String jdk =  "jdk";
    /*System.out.println(jdk.toUpperCase());
    System.out.println(jdk.toUpperCase().substring(1));*/
    StringBuffer sbr =  new StringBuffer(jdk);
    System.out.println(sbr.reverse());
}

/**
 * 写一个方法判断一个字符串是否对称 例:abccba  
参考StringBuffer的reverse()函数
 */
@Test
public void test14() 
{
    StringBuffer s1  = new StringBuffer("wanghaooagnaw");
    StringBuffer  s2 = s1.reverse();
    System.out.println(s1);
    System.out.println(s2);
    if(s1.equals(s2)) {
        System.out.println("是对称的");
    }else {
        System.out.println("不是对称的");
    }
}


@Test
public void test15() 
{
    StringBuffer s3  = new StringBuffer("ah");
    String s5 = s3.toString();

    s3.reverse();  // reverse将内容倒序  
    System.out.println(s5);
    if(s3.equals(s5)) {
        System.out.println("是对称的");
    }else {
        System.out.println("不是对称的");
    }
}
/**
 * String s=”name=zhangsan age=18 classNo=090728 ”;
 * 将上面的字符串拆分,结果如下:zhangsan 18 090728
 */
@Test
public void test16() 
{
    String str = "name=zhangsan age=18 classNo=090728";
    String[]  strayyay = str.split(" ");  //按照空格 分数组  [name=zhangsan]   [age=18]   []
    for(int i= 0;i

}

你可能感兴趣的:(JAVA基础)