import java.util.scanner public class ScannerDemo{ public static void main(String[] args){ //创建对象 Scanner sc = new Scanner(System.in); //接收数据 System.out.println("请输入一个字符串数据"); String line = sc.nextLine(); //输出结果 System.out.println("你输入的数据是:"+line); } }
import java.util.scanner public class StringDemo{ public static void main(String[] args){ String s1 = new String(); System.out.println("s1:" + s1); char[] chs = {'a','b','c'}; String s2 = new String(chs); System.out.println("s2:" + s2); byte[] bys = {97,98,99}; String s3 = new String(bys); System.out.println("s3:" + s3); String s4 = "abc" System.out.println("s4:" + s4); //推荐使用直接赋值的方式 } }
使用==做比较
使用equals()做比较
import java.util.scanner public class StringDemo02{ public static void main(String[] args){ char[] chs = {'a','b','c'}; String s1 = new String(chs); String s2 = new String(chs); String s3 = "abc"; String s4 = "abc"; System.out.println(s1==s2); System.out.println(s1==s3); System.out.println(s3==s4); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s3.equals(s4)); } }
public class StringDemo03{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串") String line = sc.nextLine(); for(int i=0; i<line.length(); i++){ System.out.println(line.charAt(i)); } } }