
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to replace a word with asterisks in a sentence
To replace a word with asterisks in a sentence, the Java program is as follows −
Example
public class Demo{ static String replace_word(String sentence, String pattern){ String[] word_list = sentence.split("\s+"); String my_result = ""; String asterisk_val = ""; for (int i = 0; i < pattern.length(); i++) asterisk_val += '*'; int my_index = 0; for (String i : word_list){ if (i.compareTo(pattern) == 0) word_list[my_index] = asterisk_val; my_index++; } for (String i : word_list) my_result += i + ' '; return my_result; } public static void main(String[] args){ String sentence = "This is a sample only, the sky is blue, water is transparent "; String pattern = "sample"; System.out.println(replace_word(sentence, pattern)); } }
Output
This is a ****** only, the sky is blue, water is transparent
A class named Demo contains a function named ‘replace_word’ that takes the sentence and the pattern as parameters. A sentence is split and stored in a string array. An empty string is defined, and the pattern is iterated over, based on its length.
An asterisk value is defined as ‘*’ and for every character in the sentence, the character is compared to the pattern and a specific occurrence is replaced with the asterisk symbol. The final string is displayed on the console.
- Related Articles
- Python Program to replace a word with asterisks in a sentence
- C# Program to replace a character with asterisks in a sentence
- PHP program to replace a word with a different symbol in a sentence
- Java program to reverse each word in a sentence
- Java program to count the characters in each word in a given sentence
- Python program to reverse each word in a sentence?
- How to replace all occurrences of a word in a string with another word in java?
- C program to Replace a word in a text by another given word
- PHP program to find the first word of a sentence
- Print longest palindrome word in a sentence in C Program
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Python program to remove all duplicates word from a given sentence.
- C++ program for length of the longest word in a sentence
- Java Program to Reverse a Sentence Using Recursion
- Java Program to convert first character uppercase in a sentence

Advertisements