Java程式演示字串插值
字串插值是一種連線技術,用於透過用變數(或後續示例中的字串)替換佔位符字元來動態或有效地顯示輸出文字。
它重構程式碼,避免了重複使用變數來生成輸出的需要。它可以透過用分配給字串的變數名替換佔位符來有效地編寫冗長的變數名或文字。
Java中的字串插值
Java中有多種方法可以使用連線運算子和庫函式或類來執行字串插值。在這裡,我們將討論四種不同的字串插值方法:
使用“+”運算子
使用format()方法
使用MessageFormat類
使用StringBuilder類
使用“+”運算子
在雙引號之外的語句中,我們可以使用+運算子操作字串。根據具體情況或上下文,變數或字串名稱應該出現在+運算子之前或之後。我們透過用變數的值替換變數來實現文字的插值或連線。
示例
以下程式演示了使用+運算子進行字串插值的用法。
// Java Program to Illustrate String Interpolation
import java.io.*;
public class StringInterpolation1 {
// Main method
public static void main(String[] args){
// String 1
String str1 = "Let us make the world";
// String 2
String str2 = " to live ";
// String 3
String str3 = " in. ";
// display the Interpolated string
System.out.println(
str1 + " a better place "
+ str2 + str3);
}
}
執行此程式碼後,將顯示以下結果:
Let us make the world a better place to live in.
使用format()方法
這種方法透過將內容與表示式和變數名分開,使文字保持簡潔易於使用短句或表示式。由於format()方法接收字串作為第一個引數,變數作為第二個引數,因此佔位符(字串的%s)按順序使用以適應提供表示式的變數的值。因此,引數將比字串佔位符多一個。
示例
以下程式演示了使用format()方法進行字串插值的用法。
// Java Program to Illustrate the working of String Interpolation
// by the means of the format () method
public class StringInterpolation2 {
public static void main(String[] args){
// String 1
String str1 = "Let us make the world";
// String 2
String str2 = "to live ";
// display the interpolated string
System.out.println(String.format("%s a better place %s in.", str1,str2));
}
}
以下是上述程式碼的輸出:
Let us make the world a better place to live in.
使用MessageFormat類
要使用提供format()方法的MessageFormat類,我們必須匯入它。除了佔位符的寫法之外,MessageFormat類中的format()方法和String類中的format()方法幾乎相同。
但是,MessageFormat類的format()方法避免了重複使用同一個變數。佔位符使用索引編寫,例如“0”、“1”、“2”等等。這比String類中的format()方法有一些優勢。
示例
以下程式演示了使用MessageFormat類進行字串插值。
// java program to illustrate string interpolation
//by the means of MessageFormat class
import java.io.*;
// Import MessageFormat class from the java.text package
import java.text.MessageFormat;
public class StringInterpolation2 {
// Main method
public static void main(String[] args) {
// assigning value in String 1
String str1 = "Have";
// String 2
String str2 = " working hard ";
// String 3
String str3 = "will";
// String 4
String str4 = "into";
// display the interpolated string
System.out.println(MessageFormat.format("{0} faith, keep {1} everything {2} fall {3} line.", str1, str2, str3, str4));
}
}
執行此程式碼後,將顯示以下輸出:
Have faith, keep working hard everything will fall into line.
使用StringBuilder類
由於其長度和低使用率,這種策略相對不常見。我們使用StringBuilder類建立一個新物件,然後使用append()方法在準備好的文字之前新增變數。此類允許根據需要連結任意數量的append過程。然而,它使程式碼難以閱讀。
示例
以下程式演示了使用StringBuilder類進行字串插值的用法。
// Java Program to illustrate String Interpolation
// by the means of StringBuilder class
import java.io.*;
public class StringInterpolation4 {
public static void main(String[] args) {
// String 1
String str1 = "Tutorials Point ";
// String 2
String str2 = "is a website that";
// String 3
String str3 = "in technical as well as non technical domain";
//display the interpolated string through
//StringBuilder class and append() method
System.out.println(
new StringBuilder(str1)
.append(str2)
.append(" offers vast range of topics ")
.append(str3)
.append("."));
}
}
執行後,此程式碼將產生以下結果:
Tutorials Point is a website that offers vast range of topics in technical as well as non technical domain.
結論
本文演示了Java中的字串插值。文章首先討論了字串插值的術語。此外,還討論了四種執行字串插值的方法及其實現。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP