API
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);
}
}
String
String构造方法
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);
//推荐使用直接赋值的方式
}
}
String 字符串的比较
使用==做比较
- 基本类型:比较的是数据值是否相同
- 引用类型:比较的是地址值是否相同
使用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));
}
}
String 字符串的遍历
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));
}
}
}
String 字符次数统计
public class StringDemo04{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:")
String line = sc.nextLine();
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
for(int i=0; i<line.length(); i++){
char ch = line.charAt(i);
if(ch>='A' && ch<='Z'){
bigCount++;
}else if(ch>='a' && ch<='z'){
smallCount++;
}else if(ch>='0' && ch<='9'){
numberCount++;
}
}
System.out.println("大写字母:" + bigCount + "个");
System.out.println("小写字母:" + smallCount + "个");
System.out.println("数字:" + numberCount + "个");
}
}
String 字符串的拼接
public class StringDemo05{
public static void main(String[] args){
int[] arr = {1,2,3};
String s = ArrayToString(arr);
System.out.println("s: " + s);
}
public static String ArrayToString(int[] arr){
String s = "";
s + = "[";
for(int i=0, i<arr.length; i++){
if(i==arr.length-1){
s + = arr[i];
}else if{
s + = arr[i];
s + = ", ";
}
}
s + = "]";
return s;
}
}
String 字符串的反转
public class StringDemo06{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串: ");
String line = sc.nextLine();
String s = reverse(line);
System.out.println("s: " + s);
}
public static String reverse(String s){
String ss = "";
for(int i = s.length()-1; i>=0; i--){
ss += s.charAt(i);
}
return ss;
}
}
StringBuilder
StringBuilder 构造方法
public class StringDemo06{
public static void main(String[] args){
StringBuilder sb = new StringBuilder();
System.out.println("sb: " + sb);
StringBuilder sb2 = new StringBuilder("hello");
System.out.println("sb2: " + sb2);
}
}
StringBuilder 添加和反转方法
public class StringDemo06{
public static void main(String[] args){
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = sb.append("hello");
StringBuilder sb3 = sb2.reverse();
System.out.println("sb: " + sb);
System.out.println("sb2: " + sb2);
System.out.println("sb3: " + sb3);
}
}
public class StringDemo06{
public static void main(String[] args){
StringBuilder sb = new StringBuilder();
sb.append("hello").append("world");
sb.reverse();
System.out.println("sb: " + sb);
}
}
StringBuilder 和 String 相互转换
public class StringDemo06{
public static void main(String[] args){
StringBuilder sb = new StringBuilder();
sb.append("hello");
String s = sb.toString();
System.out.println("s: " + s);
String s = "hello";
StringBuilder sb = new StringBuilder(s);
System.out.println("sb: " + sb);
}
}
StringBuilder 字符串的拼接
public class StringDemo05{
public static void main(String[] args){
int[] arr = {1,2,3};
String s = arrayToString(arr);
System.out.println("s: " + s);
}
public static String arrayToString(int[] arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i=0, i<arr.length; i++){
if(i==arr.length-1){
sb.append(arr[i]);
}else if{
sb.append(arr[i]).append("' ");
}
}
sb.append("]");
String s = sb.toString();
return s;
}
}
StringBuilder 字符串的反转
public class StringDemo06{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串: ");
String line = sc.nextLine();
String s = reverse(line);
System.out.println("s: " + s);
}
public static String reverse(String s){
//StringBuilder sb = new StringBuilder(s);
//sb.reverse()
//String ss = sb.toString();
//return ss;
return new StringBuilder(s).reverse().toString();
}
}
|