TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
String Replace Program in Java
Alagunila Meganathan
Aug 02
2016
Code
491
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Replace.rar
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"
);
}
}
}
java
String