Java 程序将整数和字符串连接


要将整数与字符串值连接起来,需要使用 + 操作符。

假设字符串如下。

String str = "Demo Text";

现在,我们将整数值连接到上述字符串。

String res = str + 1 + 2 + 3 + 4 + 5;

示例

 在线演示

public class Demo {
    public static void main(String[] args) {
       String str = "Demo Text";
       System.out.println("String = "+str);
       String res = 99 + 199 + 299 + 399 + str;
       System.out.println(res);
    }
}

输出

String = Demo Text
996Demo Text

更新时间:2020-06-26

468 次浏览

开启你的 职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.