package myproject; import java.util.Scanner;//用到标准输入,必添包 public class demo { public static void main(String[] args) { System.out.println("hello world"); String greeting="hello"; String s=greeting.substring(0,3); System.out.println(s);//子串 strin类的substring方法可以从一个较大的字符串中提取一个子串,注第二个参数是不想复制的第一个位置且不包括。 String expletive="PG"; int x=13; System.out.println(expletive+x); greeting=greeting.substring(0,3)+"p!"; System.out.println(greeting);//拼接 注意1、拼接对象可以是变量也可以是字符串2、当一个非字符串与一个非字符串的值拼接时,非字符串会转化成字符串 if(s.equals(expletive)) System.out.println("ture"); else System.out.println("flase");//equals 检验字符串是否相等 String y="HELP!"; if(greeting.equalsIgnoreCase(y)) System.out.println("ture"); else System.out.println("FLASE");//equalIgnore方法 检验字符串是否相等但不区分大小写 if(y.length()==0) System.out.println(1); else System.out.println(2); if(y.equals(""))//str.equals("") 检验空串 System.out.println(1); else System.out.println(2);//length方法 检验字符串长度 StringBuilder builder=new StringBuilder();//构建一个空的字符串构建器 builder.append("ch");//append方法 添加内容就调用 builder.append("zwtg"); System.out.println(builder); String completedString=builder.toString();//toString方法 字符串构建完成调用toString得到一个String对象 Scanner in=new Scanner(System.in);//构造一个标准输入流system.in关联的scanner对象 System.out.println("what's your name?"); String name=in.nextLine();//nextline方法 String fistName=in.next();//next方法 System.out.println(name); System.out.println(fistName); System.out.println("How old are you?"); int age=in.nextInt();//nextInt方法 System.out.println(age); double high=in.nextDouble();//类似的nextDouble String all=String.join("r","s","l","m");//静态join方法 第一个参数为分隔符 System.out.println(all); String repeated="java".repeat(3);//repeat方法 括号里的是重复几次 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)