本文主要是介绍java-String,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package ch3;
import org.junit.Test;
import java.util.Arrays;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What is your name?");
String name = sc.nextLine();//读取一行
String msg = sc.next();//读取一个单词
//li hua
//lihua 1
//li hua,lihua
System.out.println(name + "," + msg);
}
@Test
public void test1() {
double x = 4;
double y = Math.sqrt(x);
System.out.println(y);//2.0
double y1 = Math.pow(x, 3);
System.out.println(y1);
int a = 1000000000;//10亿
long a_L = 1000000000L;//10亿
System.out.println(a * 3);//-1294967296
System.out.println(a_L * 3);//3000000000
// System.out.println(Math.multiplyExact(a,3));//integer overflow
System.out.println(Math.multiplyExact(a_L, 3));//3000000000
System.out.println("-------------");
}
@Test
public void test2() {
String s = "I am ";
String name = "LiHua";
String message = s + name;
System.out.println(message);//I am LiHua
int num = 76;
message = s + num;
System.out.println(message);//I am 76
String all = String.join("@", "S", "M", "L", "XL");
System.out.println(all);//S@M@L@XL
String repeated = "Java".repeat(3);//Java 11才支持
System.out.println(repeated);//JavaJavaJava
String greeting = "Hello!";//String类不可变
greeting = greeting.substring(0, 3) + "p!";
System.out.println(greeting);//Help!
String hello = "Hello";//== 比较地址,equals 比较字面值
System.out.println("Hello".equals(hello));//true
System.out.println("hello".equals(hello));//false
System.out.println("hello".equalsIgnoreCase(hello));//true
System.out.println("Hello" == hello);//true
System.out.println("Hel".equals(hello.substring(0, 3)));//true
System.out.println("Hel" == hello.substring(0, 3));//false
}
@Test
public void test3(){
String str = "";
String str1 = null;
if (str.length() == 0)
System.out.println("空");
if (str.equals(""))
System.out.println("空");
if (str1 != null)
System.out.println("null");
String str2 = " Hello ";
System.out.println(str2.trim());//Hello
StringBuilder sb = new StringBuilder();
sb.append('I');
sb.append(" am LiHua");
System.out.println(sb);//I am LiHua
System.out.println(sb.toString());//I am LiHua
char[] passwd="1242".toCharArray();
String str3=String.valueOf(passwd);
System.out.println(str3);//1242
String str4= Arrays.toString(passwd);
System.out.println(str4);//[1, 2, 4, 2]
}
}
这篇关于java-String的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!