在 Java 中连接字符串

今天,我们要看的是 concat()+ 操作符之间的区别。它们都是用来连接字符串的,但我们在这里要找出它们之间的不同之处。

+ 运算符可以连接多种类型的值,而 concat() 只能连接字符串值

+concat() 的第一个大区别是,我们可以用+ 操作符用 String 连接多种数据类型,但 concat() 方法被限制为只能取一个 String 类型的值。

如果我们看一下下面的情况,当我们使用+ 连接值时,任何数据类型的每个值都会使用 toString() 方法转换为 String

下面的例子表明,我们可以将一个 int b 连接到一个 String a。但如果我们在 concat() 上尝试这样做,它将给出一个运行时错误,因为它不能接受一个 int 类型的值。

package com.company;
public class Main {
    public static void main(String[] args) {
        String a = "String A-";
        int b = 5;
        System.out.println(a+b);
    }
}

输出:

String A-5

+ 如果值是空的,会生成一个新的 String,而 concat() 则返回相同的值

接下来要记下来的最大区别是,+ 每次得到一个值时,即使其长度为零,也会生成一个新的 String。但是 concat() 只有在遇到值的长度大于零时才会生成一个新的字符串。

如果我们要比较两个字符串,这可能会改变很多结果,就像我们下面所做的那样。第一个比较是在使用 concat() 对字符串进行连接时进行的,而第二个比较则显示了用+ 连接的两个字符串的比较结果。

package com.company;
public class Main {
    public static void main(String[] args) {
        String a = "String A";
        String b = "";
        String d = a.concat(b);
        if(d == a){
            System.out.println("String d and a are equal using concat");
        }else{
            System.out.println("String d and a are NOT equal using concat");
        }
        String e = a + b;
        if(e == a){
            System.out.println("String e and a are equal using plus operator");
         }else{
            System.out.println("String e and a are NOT equal plus operator");
        }
    }
}

输出:

String d and a are equal using concat
String e and a are NOT equal plus operator

+ 将连接 nullconcat() 将抛出一个异常

在下面的例子中,我們可以看到,如果我們用 null 初始化變量 b,它仍然會正確地連接。這是因為 + 操作符將每個值轉換為 String,然後將它們連接起來。

package com.company;
public class Main {
    public static void main(String[] args) {
        String a = "String A-";
        String b = null;
        System.out.println(a+b);
    }
}

输出:

String A-null

+ 不同的是,当我们把 b 连接到 a 时,其中有 null,它会抛出一个 NullPointerException,一般来说,这是正确的输出。

package com.company;
public class Main {
    public static void main(String[] args) {
        String a = "String A-";
        String b = null;
        System.out.println(a.concat(b));
    }
}

输出:

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.lang.String.concat(String.java:1937)
	at com.company.Main.main(Main.java:14)

+ 可以连接多个值,而 concat() 只能取一个值

+ 运算符可以像我们在下面的例子中那样连接多个值。不过,由于 concat() 函数只接受一个参数,它不能连接两个以上的值。

package com.company;
public class Main {
    public static void main(String[] args) {
        String a = "String A-";
        String b = "String B-";
        String c = "String C";
        System.out.println(a + b + c);
        System.out.println(a.concat(b));
    }
}

输出:

String A-String B-String C
String A-String B-

在 JavaScript 中连接字符串和整数

每当我们用 JavaScript 开发 Web 应用程序时,我们总是会遇到这样一种情况,即我们希望将任何类型的变量(整数、浮点数等)与字符串连接起来。这是一个常见的场景。

本文解释了我们如何在 JavaScript 中使用模板字符串功能和 + 运算符将字符串值与整数值连接起来。

在 JavaScript 中使用模板字符串连接字符串和整数

JavaScript 中的模板字符串允许你在给定的字符串值中添加任何类型的变量。反引号 (`) 字符表示模板字符串。

因此,要使用此功能,我们必须将字符串包含在反引号字符中。

我们在这里不使用传统的双引号运算符 (" ")。模板字符串也称为模板文字。

模板字符串功能是在 ES6 版本的 JavaScript 中引入的。

假设我们有一个字符串和一个值为 8 的整数变量 value,如下所示。我们的目标是将变量 value 的值与字符串连接起来,然后在控制台上打印出来。

代码片段 – JavaScript:

let value = 8;
console.log("Number is the value.");

输出:

Number is the value.

为此,首先,我们必须将字符串的双引号(" ")替换为反引号字符(`)。

用反引号括起字符串后,我们可以在字符串中的任何位置连接或插入变量 value。为此,我们必须使用 ${}

在其中,我们添加变量 ${value},如下所示。

代码片段 – JavaScript:

let value = 8;
console.log(`Number ${value} is the value.`);

输出:

Number 8 is the value.

如你所见,值 8 打印在字符串中。每当我们想将字符串与变量连接时,我们都会使用字符串文字的概念。

同样,你可以使用此功能连接字符串中具有任何类型的许多变量。

在 JavaScript 中使用 + 运算符连接字符串和整数

JavaScript 中的 + 运算符还可以将字符串与整数连接起来。我们将采用与上面相同的示例,但我们将使用 + 运算符而不是模板字符串。

代码片段 – JavaScript:

let value = 8;
console.log("Number " + value + " is the value.");

输出:

Number 8 is the value.

在这里,我们必须在字符串上使用传统的双引号,并且在我们想要将整数变量 value 与字符串连接的任何位置,我们首先必须关闭双引号。然后我们在变量 value 之前和之后使用+ 运算符。

在此之后,我们可以再次在双引号的帮助下继续处理剩余的字符串,如上所示。

使用+ 运算符的唯一问题是,如果要在字符串之间连接多个变量,我们需要使用多个+ 运算符来分隔字符串,如下所示。

如果有很多变量要与字符串连接,则可能会在阅读和理解代码时产生问题。

代码片段 – JavaScript:

let value1 = 8;
let value2 = 16;
let value3 = 24;
console.log("Table of " + value1 + " has values " + value2 + " and " + value3);

输出:

Table of 8 has values 16 and 24

因此,始终建议你尽可能多地使用 ES6 的模板文字功能。如果需要与字符串连接的变量很少,那么,在这种情况下,你可以使用+ 运算符。