Java中字符串的串联
您可以使用concat()
method或+运算符组合/串联两个字符串来完成任务。
示例
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);