金额转换
package me.JavaStudy;
import java.util.Scanner;
//币值转换
public class Captial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字");
int number = sc.nextInt();
String result = "";
String[] arr = {"佰","什","万","仟","佰","什","元"};
while(number != 0){
result = captialNumber(number%10) + result;
number /= 10;
}
int count = result.length();
for(int i = count; i < 7; i++){
result = "零" + result;
}
String ans = "";
for (int i = 0; i < result.length(); i++) {
ans += result.charAt(i) + arr[i];
}
System.out.println(ans);
}
public static String captialNumber(int num){
String[] arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
return arr[num];
}
}
package me.JavaStudy;
public class StringDemo1 {
public static void main(String[] args) {
String s = "16834331202";
String start = s.substring(0,3);
String end = s.substring(7);
String result = start + "****" + end;
System.out.println(result);
}
}
package me.JavaStudy;
//replace函数
public class StringDemo2 {
public static void main(String[] args) {
String s = "你好,我是张三";
System.out.println(s);
s = s.replace("张三","李四");
System.out.println(s);
}
}
StringBulider概述
StringBuilder 可以看成一个容器,创建之后里面的内容是可变的
- 作用: 提高字符串的操作效率
使用StringBuilder的两个场景:1.字符串拼接 2.字符串反转
StringBuilder的使用案例
package me.JavaStudy;
public class StringBuliderDemo1 {
public static void main(String[] args) {
//1.创建对象 因为StringBuilder是Java已经写好的类,打印对象是属性值,而不是地址值
StringBuilder sb = new StringBuilder("abcd");
//2.添加元素
sb.append(8.8888);
int len = sb.length();
System.out.println(len);
//反转操作
sb.reverse();
System.out.println(sb);
//添加字符串
sb.append("abcdefg");
//再把StringBuilder变回字符串
String s = sb.toString();
System.out.println(s);
}
}
回文字符串
package me.JavaStudy;
import java.util.Scanner;
public class StringBuliderDemo1 {
public static void main(String[] args) {
//链式编程
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String s = sc.next();
String result = new StringBuilder().append(s).reverse().toString();
if(s.equals(result)){
System.out.println("YES");
}
else System.out.println("NO");
}
}
数组变成字符串
package me.JavaStudy;
public class StringBuliderDemo1 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
System.out.println(arrToString(arr));
}
public static String arrToString(int[] arr){
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < arr.length; i++) {
if(i == arr.length - 1)sb.append(arr[i]).append("]");
else sb.append(arr[i]).append(", ");
}
return sb.toString();
}
}
StringJoiner概述
- StringJoiner跟StringBuilder一样,也可以看成一个容器,创建之后里面的内容可以改变
- 作用: 提高字符串的操作效率,而且代码编写特别简洁,但是市面上很少有人用(JDK8出现的)
StringJoiner没有空参构造
package me.JavaStudy;
import java.util.StringJoiner;
public class StringJoinerStudy {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner("---");
sj.add("aaa").add("bbb").add("ccc");
System.out.println(sj);
StringJoiner sj2 = new StringJoiner(",","[","]");
sj2.add("1").add("2").add("3");
String s = sj2.toString();
System.out.println(s);
}
}
调整字符串内容
package me.JavaStudy;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int len = a.length();
boolean flag = check(a,b);
if(flag) System.out.println("YES");
else System.out.println("NO");
}
public static boolean check(String a,String b){
for (int i = 0; i < a.length(); i++) {
a = rotate(a);
if(a.equals(b))return true;
}
return false;
}
public static String rotate(String s){
return s.substring(1) + s.charAt(0);
}
}