"[Miren]" <boli_NOSPAM_@excite.com> wrote in message
news:...
> Hello
> i am usin jdk 1.3
> i want to replace into one string some substring, but into the jdk 1.3
there
> is not regexp and replace (string)
>
> in my string i have some substring :##---user---##
> i must to analize the string and substituthe the substring
(##---user---##)
> with other string (example my name)
> i have seen intot he jdk 1.3 the replace but is using chat.
>
> can any body helps me for replacing into one string the substring with the
> patern (##---user---##)
>
> example:
>
> String totalstring ="asdf afad adsf asdfja##---user---##sfkjl
> askjajsdlfjñsdufñasdjflñsadjfñlasjdflj asfualesrjfk ñaso9uf erj
> glos##---user---##yg sjgsapoj aeur oawejlsdj ñlsdjf lasj";
>
> how can i replace the ##--user--## substring with: ny name ?
Simple string substitution can be performed using indexOf() and some other
methods/classes. Here's an example utility method that can replace all
instances of a substring (toReplace) inside a given string (source) with a
replacement string (replacement).
public static String replaceAll(
String source,
String toReplace,
String replacement
) {
int idx = source.lastIndexOf( toReplace );
if ( idx != -1 ) {
StringBuffer ret = new StringBuffer( source );
ret.replace( idx, idx+toReplace.length(), replacement );
while( (idx=source.lastIndexOf(toReplace, idx-1)) != -1 ) {
ret.replace( idx, idx+toReplace.length(), replacement );
}
source = ret.toString();
}
return source;
}
--
David Hilsee
|