Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > replacing one string using jdk 1.3

Reply
Thread Tools

replacing one string using jdk 1.3

 
 
[Miren]
Guest
Posts: n/a
 
      08-01-2004
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


 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      08-01-2004
[Miren] wrote:
> 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() );

 
Reply With Quote
 
David Hilsee
Guest
Posts: n/a
 
      08-01-2004
"[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String replacing help Jeremy Woertink Ruby 5 04-19-2009 08:42 PM
Replacing one string with another kevin.eugene08@googlemail.com C Programming 1 07-14-2008 09:03 PM
Replacing a word in a string jacob navia C Programming 35 12-19-2005 02:20 AM
Replacing palindrome substrings of an input string with a given string Tung Chau C Programming 1 08-06-2004 07:27 PM
Replacing palindrome substrings of an input string with a given string. Any effecient algorithm? Tung Chau C Programming 0 08-06-2004 10:18 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57