当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。