Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

how to extract substring

Thread view: 
Petterson Mikael - 06 Apr 2006 08:23 GMT
Hi,

I have the following string (read from a file).

}  lrsstuv;

I need to extract the lrsstuv part. The bracket will always be there.
The number of spaces after the bracket varies. And at last the ';' is
always after there after the name. Any hints on how to do this?

I am using jdk 1.4

cheers,

//mikael
derek@gkdkwe.com - 06 Apr 2006 08:53 GMT
> I have the following string (read from a file).
>
[quoted text clipped - 3 lines]
> The number of spaces after the bracket varies. And at last the ';' is
> always after there after the name. Any hints on how to do this?

String s = "} lrsstuv;";
String result = s.substring(1, s.length()-1).trim();
Simon - 06 Apr 2006 08:59 GMT
> I have the following string (read from a file).
>
[quoted text clipped - 3 lines]
> The number of spaces after the bracket varies. And at last the ';' is
> always after there after the name. Any hints on how to do this?

try to match it against a regular expression? Try something like

 }\s*(\w+);

(no guarantee for escape characters...)
Look into the documentation for java.util.regex.Pattern.

If you are sure that the "}" and ";" are really there, you can also use
s.substring(1, s.length()-2).trim() to remove "}" and ";" and then remove the
blanks. That could be easier.
Petterson Mikael - 06 Apr 2006 09:18 GMT
>>I have the following string (read from a file).
>>
[quoted text clipped - 14 lines]
> s.substring(1, s.length()-2).trim() to remove "}" and ";" and then remove the
> blanks. That could be easier.

Last works like a charm ;-)

cheers,

//mikael
derek@gkdkwe.com - 06 Apr 2006 10:50 GMT
> >>I have the following string (read from a file).
> >>
[quoted text clipped - 16 lines]
>
> Last works like a charm ;-)

No, it does not. It also removes the last character, in your case the 'v'.
It should be '-1' instead of '-2'.
Simon - 06 Apr 2006 11:52 GMT
>>>If you are sure that the "}" and ";" are really there, you can also use
>>>s.substring(1, s.length()-2).trim() to remove "}" and ";" and then remove the
[quoted text clipped - 4 lines]
> No, it does not. It also removes the last character, in your case the 'v'.
> It should be '-1' instead of '-2'.

You are right, sorry. Second argument is endIndex, not length.
Petterson Mikael - 07 Apr 2006 08:35 GMT
>>>>If you are sure that the "}" and ";" are really there, you can also use
>>>>s.substring(1, s.length()-2).trim() to remove "}" and ";" and then remove the
[quoted text clipped - 6 lines]
>
> You are right, sorry. Second argument is endIndex, not length.

Yeah I know  but that part was so simple that I could change it myself. :-)

cheers,

//mikael
Roedy Green - 06 Apr 2006 21:42 GMT
On Thu, 06 Apr 2006 09:23:11 +0200, Petterson Mikael
<mikael.petterson@era.ericsson.se> wrote, quoted or indirectly quoted
someone who said :

>}  lrsstuv;

You could use a regex split with ; and space as split chars, or even }
too.
see http://mindprod.com/jgloss/regex.html#SPLITTING
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.