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

Replace all the occurrences of sub string in the given string

Using String.replace() method, we can replace all the matching substrings in a string.

 

replace_sub_string.py

str1 = 'hello there, hi there'

str2 = str1.replace('there', 'Krishna')

print('str1 : ', str1)
print('str2 : ', str2)

 Above snippet generate below output.

str1 :  hello there, hi there
str2 :  hello Krishna, hi Krishna

 


  

Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Replace all the occurrences of sub string in the given string

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×