replacing one string using jdk 1.3

M

[Miren]

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 ?

thanks
 
S

Sudsy

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 ?

thanks

String pattern = "##--user--##";
String replacement = "ny name";
int idx = -1;
while( ( idx = totalstring.indexOf( pattern ) ) >= 0 )
totalstring = totalstring.substring( 0, idx ) + replacement +
totalstring.substring( idx + pattern.length() );
 
D

David Hilsee

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top