首頁 > Java > java教程 > 主體

如何在 Java 中刪除字串中某個字元的所有實例?

Barbara Streisand
發布: 2024-11-14 12:35:02
原創
286 人瀏覽過

How to Remove All Instances of a Character from a String in Java?

Replacing All Instances of a Character in a String in Java

To eliminate all instances of a specific character from a String in Java, we can employ the replace() method. This method offers two variations: one that takes a single character as an argument and another that accepts a sequence of characters (such as a String).

The example provided employs the form that replaces a character with a different character. However, when the aim is to remove a character completely, we should utilize the variation that replaces the character with an empty String.

To achieve this, we modify the code as follows:

str = str.replace("X", "");
登入後複製

This modification ensures that the character "X" is replaced by an empty String, effectively removing it from the String.

以上是如何在 Java 中刪除字串中某個字元的所有實例?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板