Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - replacing one string using jdk 1.3

 
Thread Tools Search this Thread
Old 08-01-2004, 05:06 PM   #1
Default replacing one string using jdk 1.3


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




[Miren]
  Reply With Quote
Old 08-01-2004, 07:13 PM   #2
Sudsy
 
Posts: n/a
Default Re: replacing one string using jdk 1.3
[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() );



Sudsy
  Reply With Quote
Old 08-01-2004, 08:49 PM   #3
David Hilsee
 
Posts: n/a
Default Re: replacing one string using jdk 1.3
"[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




David Hilsee
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB 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
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
urgent help....need urgent help on say string task.. pooja Software 0 03-03-2009 06:16 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
ASP.NET: Asign Users in Roles(Array.IndexOf(Of String) method) msandlana Software 0 04-25-2008 06:37 AM
Hidden linebreaks in string? VB.NET Jiggy Software 0 04-23-2008 02:18 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

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