首頁 > Java > java教程 > 主體

Java 替換字串中的字符

WBOY
發布: 2024-08-30 15:36:23
原創
636 人瀏覽過

替換字串中的某個字元是指在指定字元的位置放置另一個字元。  索引代表指定的字元。在java中,String類別用於替換字元和字串。字串是java.lang包的類別。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

在程式設計中,開發人員面臨著許多需要替換字串中字元的情況。 Java提供了多種方法來替換String中的字元。刪除是替換字元的重要方法之一。使用remove方法時,應該記住一些事情

  1. Java中的字串是不可變的,因此替換字元後,會建立一個新字串。
  2. 應用replace方法之前的字串變數在應用replace方法之後保持不變。
  3. Replace 方法以新字元取代字串中的所有字元。

文法:

 在以下語法中,給出瞭如何替換字元。替換方法中有兩個參數:第一個參數是要替換的字符,第二個參數是要替換的字符。

String replace(char oldChar, char newChar):
//OR
String replaceFirst(char oldChar, char newChar):
//replaces only the first occurrence of the character
登入後複製

在第二行語法中,replaceFirst 方法用於僅替換第一次出現的字元。

Java 替換字串中的字元的範例

以下是 Java 替換字串中的字元的範例:

範例#1

在此範例中,我們可以看到如何將字串中的一個字元替換為另一個字元。

  1. 在第一行中將使用者輸入作為字串。
  2. 進一步詢問字元作為輸入以替換提供的字串。
  3. replace 方法會在下一行中使用被替換的字元建立一個新字串,因為 java 中的字串是不可變的。

代碼:

import java.util.*;
public class JavaReplaceCharExample{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Hi, please enter the String");
String str = input.nextLine();
System.out.println("Enter the character to replace");
char ch = input.next().charAt(0);
System.out.println("Enter the character to be replaced with");
char newCh = input.next().charAt(0);
String newStr = str.replace(ch, newCh);
//displaying new string after applying replace method
System.out.println(newStr);
}
}
登入後複製

輸出:

Java 替換字串中的字符

範例#2

在此範例中,replaceFirst 用於僅取代該字串中第一次出現的字元。

代碼:

import java.util.*;
public class JavaReplaceCharExample2{
public static void main(String[] args){
//second string to replace the character
String str = "All that glitters is not gold";
//displaying string before applying replace method
System.out.println("\n" + str);
//replacing character p from b
String newStr = str.replace("g", "b");
//displaying string after replacing the character
System.out.println(newStr);
//second string to replace the character
String sentence = "A cat has nine lives";
//displaying string before applying replace method
System.out.println("\n" + sentence);
//replacing character n from m
String newSentence = sentence.replaceFirst("n", "m");
//displaying new string after applying replace method
System.out.println(newSentence);
}
}
登入後複製

輸出:

Java 替換字串中的字符

程式的輸出如下圖所示。在輸出螢幕截圖中,第一個句子字元“g”被“b”取代。在第二句中,僅第一次出現的語法“n”被替換為“m”。

範例 #3

在此範例中,首先用逗號取代管道分隔值。將‘|’替換為“,”後,在下一行中使用replace方法將“A”字元替換為“i”。

代碼:

import java.util.*;
public class JavaReplaceCharExample3{
public static void main(String[] args){
//second string to replace the character
String str = "Alabama|California|Florida|Texas|New Jersey|Arizona";
//displaying string before applying replace method
System.out.println("\n" + str);
//replacing | with the comma
String newStr = str.replace('|', ',');
//displaying string after applying replace method
System.out.println("\n" + newStr);
//replacing the character A with the i
String reNewedStr = newStr.replace('A', 'i');
//displaying string before applying replace method
System.out.println("\n" + reNewedStr);
}
}
登入後複製

輸出:

Java 替換字串中的字符

範例#4

在這個例子中,我們可以看到如何在不使用replace方法的情況下替換字串。指定字元之前和之後的字串儲存在下面給出的程式中的單獨變數中。在程式中,它與要替換的字元連接起來。

代碼:

import java.util.*;
public class JavaReplaceCharExample4{
public static void main(String[] args){
//second string to replace the character
String str = "Be slow in choosing, but slower in changing.";
//displaying string before applying replace method
System.out.println("\n" + str);
int index = 3;
char chToReplacedWith = 'b';
String strBeforeChar = str.substring(0, index);
String strAfterChar  = str.substring(index + 1);
String newStr  = strBeforeChar + chToReplacedWith + strAfterChar;
//displaying string before applying replace method
System.out.println("\n" + newStr);
}
}
登入後複製

輸出:

Java 替換字串中的字符

結論

上面給出的文章描述如何替換字串中的字符,java包提供了哪些方法來處理字串。在給定的範例中,給出瞭如何使用字串類別方法來替換字串中的字元。

以上是Java 替換字串中的字符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!