![]() |
|
|
|||||||
![]() |
Java - replacing one string using jdk 1.3 |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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] |
|
|
|
|
#2 |
|
Posts: n/a
|
[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 |
|
|
|
#3 |
|
Posts: n/a
|
"[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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |