Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Replace Character in String 
 

This example replaces a character with a specified character in a given string.

 

Replace Character in String 

                         

This example replaces a character with a specified character in a given string.

To replace a character with the given character in sting first convert the string into char array. Use getChars(int scrStart, int scrEnd, char[] destChar, int destStart) method for converting a string into an array of characters. Then use while loop for comparing the "oldChar" ( character name for replace) to be change with the new character of the array. If any match find then replace the "oldChar" with newChar (character name to replace) and set flag =1. To convert charArray into string, pass "charArray" into String class.

getChars(int scrStart, int scrEnd, char[] destChar, int destStart): This method returns an array of characters from a string. We are passing four parameter into this method. First parameter scrStart is the starting point while second parameter scrEnd is the end point of the source string to convert the string into a char array. The destChar is the destined array which stores all the characters. The destStart  is starting index to store the characters. 

 The code of the program is given below:

public class Replace{
  public static void main(String s[]){
    String string="rajesh raju raja rahul ray rani ram";
    char  oldChar='r';
    char newChar='g';
    int numChar=string.length();
    char[] charArray=new char[numChar];
    string.getChars(0, numChar, charArray,0);
    int i=0,flag=0;  
    System.out.println("Your String before 
repalce\n"
+string);
    while(i<charArray.length)
    {
    if(charArray[i]==oldChar)
      {
      charArray[i]=newChar;
      flag=1;
      }
    i++;
    }
    if(flag==1)
      {
      System.out.println("\nYour String after 
repalceing 'h' with 'j'"
);
      String newString=new String(charArray);
      System.out.println(newString+"\n\nYour 
char has been replaced"
);
      }
    if(flag==0)
      {
      System.out.println("\nThe char not found");
      }      
  }
}

The output of the program is given below:

C:\replace>javac Replace.java
C:\replace>java Replace
Your String before repalce
rajesh raju raja rahul ray rani ram
Your String after repalceing 'h' with 'j'
gajesh gaju gaja gahul gay gani gam
Your char has been replaced

Download this example.

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

string s1="one\two\three";

how convert to string like
"one/two/three"

Posted by nagesh on Thursday, 12.4.08 @ 22:36pm | #82410

String aStr = "Hello";
if (aStr.replace('H','H') == "Hello")

When we execute this particular above statement.This Returns True.

if (aStr.replace("H","H") == "Hello")
When Yu Know I execute this Its not returning true,its the other way its false.

HELP IF ANY ONE CAN....



Posted by Krishna on Sunday, 09.21.08 @ 18:38pm | #80578

I want to know how to replace a character more than one.

for example. alphabet from a-z then you'll change all of that into a symbol.

could that be possible? Thanks.

Posted by monique on Thursday, 09.18.08 @ 17:29pm | #80317

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.