将变量添加到现有文本中可以通过字符串拼接或者字符串格式化来实现。
var name = "John";
var age = 25;
var message = "My name is " + name + " and I am " + age + " years old.";
console.log(message);
name = "John"
age = 25
message = "My name is " + name + " and I am " + str(age) + " years old."
print(message)
String name = "John";
int age = 25;
String message = "My name is " + name + " and I am " + age + " years old.";
System.out.println(message);
var name = "John";
var age = 25;
var message = `My name is ${name} and I am ${age} years old.`;
console.log(message);
name = "John"
age = 25
message = "My name is {} and I am {} years old.".format(name, age)
print(message)
String name = "John";
int age = 25;
String message = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(message);
无论是字符串拼接还是字符串格式化,都可以将变量的值动态地添加到现有文本中,方便在程序中生成需要的输出。
领取专属 10元无门槛券
手把手带您无忧上云