public class test {
public static void main(String[] args)
{
System.out.println("I ");
System.out.println("Love ");
System.out.println("China ");
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=53568 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I
Love
China
Process finished with exit code 0
public class test {
public static void main(String[] args)
{
System.out.print("I ");
System.out.print("Love ");
System.out.print("China ");
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=53589 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Df ile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I Love China
Process finished with exit code 0
4 . 为了显示双引号,java创建一个特殊编码",可放到字符串中,字符串中遇到改编码,将其替换成双引号。
public class test {
public static void main(String[] args)
{
System.out.println("I Love \"china\"");
}
}
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=53709 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I Love "china"
Process finished with exit code 0
public class test {
public static void main(String[] args)
{
String a="I Love ";
String b="China";
System.out.println(a+b);
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54246 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
I Love China
Process finished with exit code 0
“+”还可以将字符串与变量连接起来
代码:
public class test {
public static void main(String[] args)
{
int c=2020;
String a=" I Love ";
String b="China";
System.out.println(c+a+b);
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54369 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
2020 I Love China
Process finished with exit code 0
1.比较两个字符串是否相等,可以使用equals()语句; 其值返回ture或者false
代码:
public class test {
public static void main(String[] args)
{
int c=2020;
String a=" I Love ";
String b="China";
System.out.println(a.equals(b));
}
}
输出结果
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54769 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
false
Process finished with exit code 0
public class test {
public static void main(String[] args)
{
int len;
String a="I Love China";
len=a.length();
System.out.println(len);
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=54858 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
12
Process finished with exit code 0
3.改变字符串的大小写
java提供了将字符串全部转换成大写的方法:
(toUpperCase())
和全部转换成小写的方法:
(toLowerCase());
代码如下:
public class test {
public static void main(String[] args)
{
String a="I LOVE CHINA";
String b=a.toLowerCase();
System.out.println(b);
String d="fanqiuqiu i miss you";
String f=d.toUpperCase();
System.out.println(f);
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=55060 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
i love china
FANQIUQIU I MISS YOU
Process finished with exit code 0
4.查找字符串
要在字符串中查找,可以使用indexof() ,并将要查找到的内容放到括号之中,如果未找到字符串,返回-1,如果找到,indexof()返回一个整数值,指出该字符串的起始位置。
代码示例:
public class test {
public static void main(String[] args)
{
String a="I LOVE CHINA ,fanqiuqiu i miss you";
String b="miss";
String c="you";
String d="LOVE";
int bnum=a.indexOf("miss");
System.out.println(bnum);
System.out.println(a.indexOf(c));
System.out.println(a.indexOf(d));
}
}
输出结果:
D:\eclipse\jdk\bin\java.exe -Didea.launcher.port=55492 "-Didea.launcher.bin.path=D:\java_idea\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\java_idea\out\production\java_idea_source;D:\java_idea\IntelliJ IDEA 2019.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 test
26
31
2
Process finished with exit code 0
好了今天小飞就写到这里了,