Java 程序:使用特定字符替换字符串空格

原文:https://www.studytonight.com/java-programs/java-program-to-replace-the-spaces-of-a-string-with-a-specific-character

在本教程中,我们将学习如何用特定字符替换字符串的空格。但是在进一步深入之前,如果你不熟悉字符串的概念,那么一定要查看 Java 中Strings的文章。

输入:输入字符串:一鸟在手胜于两鸟在林

输入字符:-

输出:用特定字符替换字符串的空格后:

一鸟在手胜过两鸟在林

程序 1:用特定字符替换字符串中的空格

在这个程序中,我们将看到当值在程序中被预定义时,如何用特定的字符替换字符串的空格。这里,我们将使用 replace()方法用给定的指定字符替换空白。

算法

  1. 开始
  2. 声明一个字符串。
  3. 初始化它。
  4. 在用指定字符替换空格之前,打印输入的字符串。
  5. 定义要替换空格的字符。
  6. 使用 replace()方法用给定的指定字符替换空格。
  7. 现在,在用指定字符替换空格后,打印输入的字符串。
  8. 停下来。

下面是相同的 Java 语言代码。

//Java Program to replace the spaces of a string with a specific character
public class Main   
{    
    public static void main(String[] args) 
    {    
        String string = "Actions speak louder than words";    
        char ch = '-';            
        System.out.println("String before replacing spaces with given character: ");    
        System.out.println(string);         
        //Replace space with specific character ch    
        string = string.replace(' ', ch);                
        System.out.println("String after replacing spaces with given character: ");    
        System.out.println(string);    
    }    
}

用给定字符替换空格前的字符串: 行动胜于言语 用给定字符替换空格后的字符串: 行动胜于言语

程序 2:用特定字符替换字符串中的空格

在这个程序中,我们将看到当值在程序中被预定义时,如何用特定的字符替换字符串的空格。这里,我们将使用 replaceAll()方法用给定的指定字符替换所有空白。

算法

  1. 开始
  2. 声明一个字符串。
  3. 初始化它。
  4. 在用指定字符替换空格之前,打印输入的字符串。
  5. 定义要替换空格的字符。
  6. 使用 replaceAll()方法用给定的指定字符替换空格。
  7. 现在,在用指定字符替换空格后,打印输入的字符串。
  8. 停下来。

下面是相同的 Java 语言代码。

//Java Program to replace the spaces of a string with a specific character
public class Main   
{    
    public static void main(String[] args) 
    {    
        String string = "Slow and steady wins the race";    
        char ch = '-';           
        System.out.println("String before replacing spaces with given character: ");    
        System.out.println(string);         
        //Replace space with specific character ch    
        string = string.replaceAll(" ", "-");               
        System.out.println("String after replacing spaces with given character: ");    
        System.out.println(string);    
    }    
}

用给定字符替换空格前的字符串: 慢而稳赢得比赛 用给定字符替换空格后的字符串: 慢而稳赢得比赛

程序 3:用特定字符替换字符串中的空格

在这个程序中,我们将看到当值在程序中被预定义时,如何用特定的字符替换字符串的空格。这里,我们将使用 for 和 if 循环用给定的指定字符替换空白。

算法

  1. 开始
  2. 声明一个字符串。
  3. 初始化它。
  4. 在用指定字符替换空格之前,打印输入的字符串。
  5. 定义要替换空格的字符。
  6. 对相同的使用 for 和 if 循环。
  7. 使用 for 循环迭代字符串的每个字符。
  8. 使用 if 循环检查是否存在任何空间。
  9. 如果遇到任何空格,则用指定的字符替换它。
  10. 现在,在用指定字符替换空格后,打印输入的字符串。
  11. 停下来。

下面是相同的 Java 语言代码。

//Java Program to replace the spaces of a string with a specific character
public class Main
{ 
    // Function to replace Space with - 
    static String replaceStr(String str) 
    {           
        String s = "";           
        // Iterate over each character of the string
        for (int i = 0; i < str.length(); ++i) 
        {       
            // If a space encounters then replace it with -
            if (str.charAt(i) == ' ')  
                s += '-';               
            else
                s += str.charAt(i);               
        } 
        // return the new string. 
        return s; 
    }       
    //Driver Code  
    public static void main(String []args) 
    { 
        // Initialize the String 
        String str = "There are other fish in the sea"; 
        System.out.println("String before replacing spaces with given character: ");    
        System.out.println(str);    
        //Print the modified string
        System.out.println("String after replacing spaces with given character: ");    
        System.out.println(replaceStr(str));     
    } 
}

用给定字符替换空格前的字符串: 海里还有其他鱼 用给定字符替换空格后的字符串: 海里还有其他鱼