Thursday 15 March 2012

java - How to replace a part of a string with another part? -



java - How to replace a part of a string with another part? -

i have long string ka01f332-9:25,ka01f212-9:27,ka01f242-9:35,ka01f232-9:45,. these combination of vehicle no , time. want replace time of ka01f242 10:20 how can this.

so far hav done this.

string buseat=ka01f332-9:25,ka01f212-9:27,ka01f242-9:35,ka01f232-9:45,; buseat.substring(buseat.indexof(vno),buseat.indexof(','));

but not getting exact value .and has done dynamically can 1 help me in this.

you utilize regex :

string s = "ka01f332-9:25,ka01f212-9:27,ka01f242-9:35,ka01f232-9:45,"; system.out.println(s.replacefirst("(?<=ka01f242-).*?(?=,)", "10:20")); // positive look-behind "ka01f242" , positive look-ahead "," . matched not captured, not replaced.

o/p:

ka01f332-9:25,ka01f212-9:27,ka01f242-10:20,ka01f232-9:45,

edit :

"(?<=ka01f242-).*?(?=,)", "10:20") --> first looks character preceeded "ka01f242" (positive look-behind), selects characters ("ka01f242" matched, not selected.) next, successive characters selected until comma (which agin matched, not selected)

java string replace

No comments:

Post a Comment