首页 > Java > java教程 > 正文

Java 替换字符串中的字符

WBOY
发布: 2024-08-30 15:36:23
原创
635 人浏览过

替换字符串中的某个字符是指在指定字符的位置放置另一个字符。  索引代表指定的字符。在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学习者快速成长!