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);
广告