aspose file tools
The moose likes Java in General and the fly likes How to extract a substring of a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to extract a substring of a string?" Watch "How to extract a substring of a string?" New topic
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 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

    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.
     
    IntelliJ Java IDE
     
    subject: How to extract a substring of a string?
     
    Threads others viewed
    Strings,trim
    what is this doing?
    csv file
    Regex pattern
    IntelliJ Idea IDE not supporting String
    developer file tools