不同的方式連線在 Java 中的字串


你可以在 Java 中透過使用 concat() 方法或透過使用 ‘+’,即 “連線”運算子連線兩個字串。

示例

public class ConncatSample {
   public static void main(String []args) {
      String s1 = "Hello";
      String s2 = "world";
      String res1 = s1.concat(s2);
      String res2 = s1+s2;
      System.out.print("Concatenation using method:: ");
      System.out.println(res1);
      System.out.print("Concatenation using operator:: ");
      System.out.println(res2);
   }
}

產出

Concatenation using method:: Helloworld
Concatenation using operator:: Helloworld

更新於:26-Feb-2020

471 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.