| Author |
How to extract a substring of a string?
|
Ajoo Bar
Greenhorn
Joined: Mar 28, 2011
Posts: 26
|
|
Hi,
I have a string which I need to trim
For Example the below string I need to trim (COM-ID 323391) and only have 3432343 ABc Plc.
3432343 ABc Plc (COM-ID 323391)
Thanks
Ajoo
|
 |
Koen Aerts
Ranch Hand
Joined: Feb 07, 2012
Posts: 339
|
|
Maybe regular expressions can help you here. Either via the replaceAll or the replaceFirst methods on your String object, or for more complex regular expressions you can also use the Matcher and Pattern classes from the java.util.regex package.
In your particular case, the following may achieve what you're looking for:
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3144
|
|
Ajoo Bar wrote:Hi,
I have a string which I need to trim
For Example the below string I need to trim (COM-ID 323391) and only have 3432343 ABc Plc.
3432343 ABc Plc (COM-ID 323391)
Thanks
Ajoo
You have to be more specific than that. I can come up with different solutions that will handle this, but may not be correct for your requirements.
Get rid of everything between parensGet rid of everything between FIRST pair of parensKeep first 15 charactersGet rid of last 15 charactersGet rid of everything after third whitespace
and so on and so on.
You need to describe your rules precisely.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3144
|
|
Koen Aerts wrote:
In your particular case, the following may achieve what you're looking for:
Koen, please note that this site is NotACodeMill.(⇐click) Handing somebody an answer does not help him to learn.
|
 |
Ajoo Bar
Greenhorn
Joined: Mar 28, 2011
Posts: 26
|
|
Thanks, i will try it.
Jeff. In my case it is garanteed that in the paranthesis I will have this string pattern (COM-ID 323391)
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 8428
|
|
An example is not a spec. it may hint at what needs to be done, but it doesn't tell you. You will need to define what EXACTLY you need to do. That usually requires you to think about it a lot and use pencil and paper to sketch out ideas.
Most of the time spent writing code is actually spend NOT writing code, but figuring out what the code needs to do in all cases.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: How to extract a substring of a string?
|
|
|