diff src/goodjava/util/GoodUtils.java @ 1778:1725fdb6c4f5

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Aug 2023 10:44:56 -0600
parents 2f4c99c02436
children
line wrap: on
line diff
--- a/src/goodjava/util/GoodUtils.java	Thu Aug 10 16:24:20 2023 -0600
+++ b/src/goodjava/util/GoodUtils.java	Fri Aug 11 10:44:56 2023 -0600
@@ -69,4 +69,22 @@
 		return list.subList(0, resultSize).toArray(result);
 	}
 
+	// faster than String.replace()
+	public static String replace(String s,String target,String replacement) {
+		int i = s.indexOf(target);
+		if( i == -1 )
+			return s;
+		int starting = 0;
+		int n = target.length();
+		StringBuilder sb = new StringBuilder();
+		do {
+			sb.append(s,starting,i);
+			sb.append(replacement);
+			starting = i + n;
+			i = s.indexOf(target,starting);
+		} while( i != -1 );
+		sb.append(s,starting,s.length());
+		return sb.toString();
+	}
+
 }