當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java String concat()用法及代碼示例


Java字符串concat()方法將一個字符串連接到另一個字符串的末尾。此方法返回一個字符串,該字符串的值將傳遞給該方法,並附加到該字符串的末尾。簽名:

 public String concat(String anostr)   

參數:

 anostr- string to be concatenated at the
  end of the another string.

返回:


  combination of two strings is returned.

例:展示concat()方法的用法原理

// Java program to demonstrate 
// working of concat() method 
  
class Gfg { 
    public static void main(String args[]) 
    { 
        String s = "Gfg"; 
        s = s.concat("! is the best."); 
        System.out.println(s); 
    } 
}

輸出:

Gfg! is the best.
// Java program to demonstrate 
// working of concat() method 
  
class Gfg { 
    public static void main(String args[]) 
    { 
        String s = "Gfg"; 
  
        // We must explicitly assign result 
        // of s.concat() to s.  Since not  
        // assigned to s, s does not change. 
        s.concat("! is the best."); 
  
        System.out.println(s); 
    } 
}

輸出:

Gfg


相關用法


注:本文由純淨天空篩選整理自Niraj_Pandey大神的英文原創作品 Java String concat() with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。