Java 中字符串连接
你可以使用 concat() 方法或 + 运算符连接/串联你的两个字符串来完成你的任务。
示例
import java.util.Scanner; public class ConncatSample { public static void main(String []args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your first name"); String firstName = sc.nextLine(); System.out.println("Enter your last name"); String lastName = sc.nextLine(); String completeName = firstName+lastName; System.out.print("Hi your complete name is :: "); System.out.println(completeName); } }
输出
Enter your first name KrishnaKasyap Enter your last name Bhagavatula
嗨,你的全名是− KrishnaKasyapBhagavatula
你还可以像这样使用 concat() 方法−
String completeName = firstName.concat(lastName);
广告