r/javahelp • • May 23 '20

Recommendations on String manipulation

A company I work with is replacing an old COBOL process with Java.

Pretty much the business rules are about reading some long lines from a text file, do some snippin and reordering before writing to a new file text.

That part is covered, but I'm doing some heavy use of String.substring. I haven't seen a real input file, but they are supposed to hold near 2 million lines each.

Because of that, I would like to make the new process as memory efficient as possible. (When I first learned Java they always warned us about the perils of String)

The process will run on a server (don't know the exact specs) if that's useful.

Thanks in advance.

17 Upvotes

11 comments sorted by

View all comments

8

u/Philboyd_Studge May 23 '20

Creative use of regex might do the trick, but for pure speed and memory usage, using char arrays would probably be the best.

2

u/frostbyte650 May 23 '20

Does java not treat strings as char arrays?

1

u/Yithar Intermediate Brewer May 23 '20

Internally String uses them but as said strings are immutable. That means more memory usage.