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.