VB icon

Java String Replace

Email
Submitted on: 1/2/2015 6:59:00 AM
By: Martyn B (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: Java (JDK 1.1)
Views: 1792
 
     This piece of code takes three arguments, you give it a string that you want to do the replace on, what you want to replace, and what you want to replace it with! Simple!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Java String Replace
// Description:This piece of code takes three arguments, you give it a string that you want to do the replace on, what you want to replace, and what you want to replace it with! Simple!
// By: Martyn B (from psc cd)
//
// Inputs:String to do replace on,
String to search for,
String to replace search String with
//
// Returns:String
//**************************************

// Example:
// replace("He/\$llo", "/\$", " ")
// it will return "He llo"
public static String replace(String checkMe, String toberep, String repwith)
	{
	String temp = checkMe;
	int a = 0;
	try {
			for(int i = 0; i < checkMe.length(); i++) 
			{
				
				a = temp.indexOf(toberep);
				temp = temp.substring(0 , a) + repwith + temp.substring((a + toberep.length()));
				if (a == -1)
				{
				
				break;
				}
			}
		} catch (Exception e) {/*IGNORE*/}		
	
		return temp;
	}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.