truncating String
I am trying to truncate and append to a String using the following code, which works.
publicstaticvoid main(String[] args){
String checkThisOne ="A means of modern warfare the world had complacently come to see as at least informally off the table is now very firmly back on it ?or";
String infoTrunc ="HelloWorld";
System.out.println(truncateToFitLength(checkThisOne, infoTrunc));
System.out.println(truncateToFitLength(checkThisOne, infoTrunc).length());
}
privatestatic String truncateToFitLength(String checkThisOne, String infoTrunc){
String tempVar = checkThisOne.substring(0,(55-infoTrunc.length()));
return tempVar.concat(infoTrunc);
}
But at the same time I know that Strings are expensive, so want somebody to have a second look and advise, how should I modify to make it more efficient in terms of memory.
[1247 byte] By [
AndreaGyma] at [2007-11-26 15:39:43]

You could use a StringBuilder in truncateToFilLength(), but unless you're calling this method many thousands of times, it's not likely to ever be a problem. If you profile your app and determine that the string concatenation is a bottleneck, then worry about it.
> But at the same time I know that Strings are
> expensive,
They are?
> so want somebody to have a second look and
> advise, how should I modify to make it more efficient
> in terms of memory.
Well, first of all your hardcoded length of "55" is going to be a problem when you truncate a String of a different size. I'm kind of wondering what the point of your method is, you want the first String to have the same length but end with the second String?
> > But at the same time I know that Strings are
> > expensive,
>
> They are?
I think he means that doing string concatenation creates new String objects instead of using the already created objects. Creating a large amount of new String objects could get expensive.
> I'm kind of wondering what the point of your method is, you want the first String
> to have the same length but end with the second String?
Yes or if infoTrunc is longer than 55. I too wonder what is going on here.
PS: Yuck, another post W-I-D-E post that I need to keep on scrolling left and right to be able to read.
~Bill
thanx for the responses but unfortunately I am still on jdk 1.4. So i guess my only choice would be StringBuffer. I would appreciate if I could get some sample code which could help me understand it better.
W/o knowing more about what exactly you want to do, something like this is about my best guess:
public static void main(String[] args) {
StringBuffer checkThisOne = new StringBuffer("A means of modern warfare the world had "
+"complacently come to see as at least informally "
+"off the table is now very firmly back on it ?or";
String infoTrunc = "HelloWorld";
checkThisOne= truncateToFitLength(checkThisOne, infoTrunc);
System.out.println(checkThisOne);
System.out.println(checkThisOne.length());
}
private static StringBuffer truncateToFitLength(StringBuffer _checkThisOne, String _infoTrunc) {
int infoLength = _infoTrunc.length();
int checkLength = (infoLength > 55) ? infoLength : infoLength - 55;
_checkThisOne.setLength(checkLength);
return ( _checkThisOne.append(infoTrunc) );
}
> I would appreciate if I could get some sample code which could help me
> understand it better.
There's a TechTip dealing with string concatenation and StringBuffer here:
http://java.sun.com/developer/JDCTechTips/2002/tt0305.html
And dubwai's comments at java.net here:
http://wiki.java.net/bin/view/Javapedia/AlwaysUseStringBufferMisconception
My interpretation of your code would be thus:
static String truncateAndConcatenate(String first, String second) {
StringBuffer buffer = new StringBuffer(first);
buffer.replace(first.length() - second.length(), first.length(), second);
return buffer.toString();
}
Of course, I doubt that's much faster. Perhaps if you elaborate on what you're trying to accomplish we can suggest something better.
> PS: Yuck, another post W-I-D-E post that I
> need to keep on scrolling left and right to be able
> to read.
Are you using Internet Explorer, then? I'm using Firefox and it puts the code in its own box with a horizontal scroll bar. And it makes the regular text have the normal page width. (Which is the width of my screen plus the width of that "Java Forums" box at the left, due to shoddy design. Should just be the width of my screen. But I can scroll right and see the whole thing in one window.)
On the other hand if you get more code than the height of the screen, and it goes in a box with a horizontal scroll bar, it gets very hard to look at the code. Sometimes you can't win.
HuH! I am using FireFox, but the code and the text all go into the same "box" and the that box is as wide as the code is (until an E-O-L char is found). So in this case it's about three screens wide - with a horizontal scrollbar.~BillNope ... make that 2 screens wide.
On FF 2.0.0.1 I don't see any d@mn boxes.
I was going to post before, but didn't want to hijack this thread. However, it
doesn't look like it's going anywhere.
DrClap: are you using any scripts that might alter the formatting? (These were
in vogue a while back for eliminating the posts of various forum entities.)