comparison 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
comparison
equal deleted inserted replaced
1777:e59349d53fec 1778:1725fdb6c4f5
67 } 67 }
68 String[] result = new String[resultSize]; 68 String[] result = new String[resultSize];
69 return list.subList(0, resultSize).toArray(result); 69 return list.subList(0, resultSize).toArray(result);
70 } 70 }
71 71
72 // faster than String.replace()
73 public static String replace(String s,String target,String replacement) {
74 int i = s.indexOf(target);
75 if( i == -1 )
76 return s;
77 int starting = 0;
78 int n = target.length();
79 StringBuilder sb = new StringBuilder();
80 do {
81 sb.append(s,starting,i);
82 sb.append(replacement);
83 starting = i + n;
84 i = s.indexOf(target,starting);
85 } while( i != -1 );
86 sb.append(s,starting,s.length());
87 return sb.toString();
88 }
89
72 } 90 }