README.md

December 4, 2022 · View on GitHub

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!

More Info

String to do replace on,

String to search for,

String to replace search String with

String

Submitted On
ByMartyn B
LevelBeginner
User Rating3.3 (13 globes from 4 users)
CompatibilityJava (JDK 1.1)
CategoryString Manipulation
WorldJava
Archive File

Source Code

// 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;
	}