GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Replacing Strings.... without StringBuffer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replacing Strings.... without StringBuffer?" Watch "Replacing Strings.... without StringBuffer?" New topic
Author

Replacing Strings.... without StringBuffer?

Andrew M.
Greenhorn

Joined: Apr 07, 2004
Posts: 6
Okay...
So, I'm doing an assignment for my java class, and our teacher has written a halstead metrics program for us as a template.
We're to modify the program to apply to the java language, i.e. take a simple program and count all the different "verbs" and "nouns", then compute the halstead metrics for it.
The problem is recognizing symbols that do not have spaces surrounding them, i.e. args[], I need to count args, [, and ]. Now, is there anyway to write a file containing all the same information while putting spaces before and after such symbols... it's not implausable to write an if statement to find a [ and put spaces on either side.
But, because of how he's written the template using StringBuffer is going to be a massive amount of work and re-design of the template, which is to be avoided. And, replace() only works with characters... so... any ideas?
Thanks!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I'm not sure whether I understood your task, but I guess you're mixing 'alphabetic characters' and 'char' from a programmers view.
If you get a line of code to parse, and firstly count the special characters, like ,;[](...;
In the next step you could replace every of them with a blank, wich is a char from a programmers view.
Then all your tokens should be seperated.


http://home.arcor.de/hirnstrom/bewerbung
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I don't know what a halstead metrics program is, but if you just want to put spaces around non-word characters [A-Za-z0-9], try this (needs java v1.4+)
Andrew M.
Greenhorn

Joined: Apr 07, 2004
Posts: 6
...
Okay... this is pretty much what it's boiling down to now...
I have one large string... and within that string are, for an example, right and left parenthesis... I have to put spaces," ", before and after every one of them.
I'm not sure I quite grasp that above block of code, but, I'll give it a whirl.
Andrew M.
Greenhorn

Joined: Apr 07, 2004
Posts: 6
Oh...
and, will replaceAll work with a normal string aswell as a stringbuffer?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
replaceAll() is a method of the String class
to work with a StringBuffer you would need to call the toString() method first
if all you need is to find parentheses/braces/brackets ()/{}/[]
try this (delete any sets not required)
str = str.replaceAll("[\\[\\]\\{\\}\\(\\)]"," $0 ");
Andrew M.
Greenhorn

Joined: Apr 07, 2004
Posts: 6
that single line of code means wonders to me, thank you.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Andrew M.,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
We'd like more than an initial for the last name.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
GeeCON Prague 2014
 
subject: Replacing Strings.... without StringBuffer?