Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

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

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

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

Latest Searches:
Photoshop Effects Lase
cashier
data retrive from data
pdfbox
call தà¯?ா?தà¯?ï
tree view in js
add items in map
left outer join+hibern
validate user input in
what is CLDC
servlets code for bidd
retrive data from data
Program to compare exc
swing
scroll bar
breakà¹?à¸??à¹?à¸???à¹
2 resultset
sample java program to
how to iterate a linke
add two numbers in jav
java program to check
ASP MS Word and ASP Dy
JSP templates. Use JSP
ะ ย ะà¸???ะÂ
PHP Web Traffic Analys
combo box field call a
summing digits in an i
Java / extending threa
java collections
ascii java
WEBSERVICE USING APACH
Matrix GUI Example in
java ั?ะà¸?à¹????à
Creating Midlet Applic
Java
servlets CRm projects
runnable
socket programming
download
Action Errors
jquery.history_remote.
select a row in jsp
java String class
h:inputtext tag in jsf
Photoshop Textures and
display the result in
Jini's relevance emerg
Welcome to the WfMOpen
ejb example, jboss
page attribute
Photoshop Abstracts Ra
simple sample program
accessing data inJSF
relationalioperator
page attrtibute
Photoshop Photo Effect
timestamp
properties of set
spring internation
Linux Caixa M�°�???
pascals triangle
DEMO APPLICATION
tree view
gui
ascii
validatorform
multiple table pdf i
summing an integer
Photoshop Web Layouts
cache
ั?ะ?ะ?ั?ะ???
hash mapping
how to create a textbo
simple sample program
jboss connection pool
java for loops
change frame font colo
types of hibernate
Javascript Date and Ti
java program to find s
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.