• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to extract a substring of a string?  RSS feed

 
Ajoo Bar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 parens
  • Get rid of everything between FIRST pair of parens
  • Keep first 15 characters
  • Get rid of last 15 characters
  • Get rid of everything after third whitespace


  • and so on and so on.

    You need to describe your rules precisely.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!