Java connection string
•
Java
Using join operators
Example 1
public static void main(String[] args) { int[] no=new int[]{501,101,204,102,334}; //定义学号数组 String[] names=new String[]{"张城","刘丽丽","李国旺","孟红霞","贺宁"}; //定义姓名数组 String[] classes=newString[]{"数学","语文","数学","英语","英语"}; //定义课程数组 System.out.println("本次考试学生信息如下:"); //循环遍历数组,连接字符串 for(int i=0;i<no.length;i++) { System.out.println("学号:"+no[i]+"|姓名:"+names[i]+"|课程:"+dasses[i]+"|班级:"+"初二(三)班"); } }
本次考试学生信息如下: 学号:501|姓名:张城|课程:数学|班级:初二(三)班 学号:101|姓名:刘丽丽|课程:语文丨班级:初二(三)班 学号:204|姓名:李国旺|课程:数学|班级:初二(三)班 学号:102|姓名:孟红霞|课程:英语|班级:初二(三)班 学号:334|姓名:贺宁|课程:英语|班级;初二(三)班
String str="Welcome to"+"Beijing"+"欢迎来到"+"北京。"+"北京我的故乡。";
Use the concat () method
字符串 1.concat(字符串 2);
Example 2
public static void main(String[] args) { String info="三国演义、"; info=info.concat("西游记、"); info=info.concat("水漭传、"); info=info.concat("红楼梦"); System.out.println(info); String cn="中国"; System.out.println(cn.concat("北京").concat("海淀区").concat("人民公园")); }
三国演义、西游记、水浒传、红楼梦 中国北京海淀区人民公园
Connect other types of data
Example 3
public static void main(String[] args) { String book="三国演义"; //字符串 int price=59; //整型变量 float readtime=2.5f; //浮点型变量 System.out.println("我买了一本图书,名字是:"+book+"\n价格是:"+price+"\n我每天阅读"+readtime+"小时"); }
我买了一本图书,名字是:三国演义 价格是:59 我每天阅读2.5小时
System.out.println("我买了一本图书,名字是:"+book+"\n 价格是:"+price+"\n我每天阅读"+(price+readtime)+"小时");
我买了一本图书,名字是:三国演义 价格是:59 我每天阅读61.5小时
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码