Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java String search and replace

Problem Statement : To search and replace a pattern of String with another.

Algorithm

1) Start.
2) Initialize the three strings s1,s2,and s3.
3) Read the string.
4) Enter the pattern to be searched.
5) Check whether the given pattern in the string using
6) s1.substring(s2).
7) Replace the pattern with another string using
8) s1=s1.concat(s2).
9) Print the result.
10) Stop.

Program
import java.io.*;

public class StrDemo 
{
	public static void main(String args[]) 
	{
	     DataInputStream in= new DataInputStream(System.in);
	     String str,chstr,repstr,newstr="",substr="";
	    int len,chlen,idx=0;
	    try
	    {
	    	System.out.println("\nEnter the string : ");
	        str=in.readLine();
	        len=str.length();
	        System.out.println("\nEnter the sub string to be replaced : ");
	        chstr=in.readLine();
	        chlen=chstr.length();
	        System.out.println("\nEnter the new sub string : ");
	        repstr=in.readLine();
	        while(len>idx)
	        {
	        	if(idx+chlen 
Output

Enter the string :
abcdabccabda

Enter the sub string to be replaced :
ab

Enter the new sub string :
ka

The resulting string is : kacdkacckada

Java String search and replace is a post from ShoutToWorld - Let's Learn Let's Shout - Helping bloggers and developers.



This post first appeared on ShoutToWorld, please read the originial post: here

Share the post

Java String search and replace

×

Subscribe to Shouttoworld

Get updates delivered right to your inbox!

Thank you for your subscription

×