dartmanx
06-08-2005, 10:00 AM
I have a (rather ugly) bit of code that handles a String that I'm trying to reformat.
One of the problems is that the "page" that the String holds has a list of text in an odd format:
text
text
text
(etc)
In other words, there are two newlines after each character. I would like to replace them with a single newline. I know they are newlines (hex code 0A), but the replaceAll() method for the String class isn't working.
Any help out there?
Jason
int arraySize = inventoryPages.length;
for (int i=0; i<arraySize; i++) {
String page = inventoryPages[i];
//System.out.print(page);
// Kill the blank lines in the string and replace them with one newline
page.replaceAll("\\u000A","|");
page.replaceAll("||","\\u000A");
System.out.print(page);
// Split the string into a List delimited by a \n (one element per line)
List lines = Arrays.asList(page.split("\n"));
// Get the initial size of the list
int listSize = lines.size();
// remove the last 15 lines of cruft, then the first line
for (int j = listSize; j>=listSize-14; j--) {
lines.remove(j);
}
lines.remove(0);
// Keep removing line 14 until it deletes what used to be line 27
for (int j= 14; j<=27; j++) {
lines.remove(14);
}
// Delete line 70 - 83
for (int j=70;j<=83; j++) {
lines.remove(70);
}
// Delete line 83 - The end
listSize = lines.size();
for (int j=83; j<listSize; j++) {
lines.remove(83);
}
// Create a new StringBuffer object, then append the remaining lines and
// the \n delimeter back to it
StringBuffer tempSB = new StringBuffer();
for (int j=0; j<lines.size(); j++) {
tempSB.append((String) lines.get(j)).append("\n");
}
// Update the information in the element
inventoryPages[i] = tempSB.toString();
}
One of the problems is that the "page" that the String holds has a list of text in an odd format:
text
text
text
(etc)
In other words, there are two newlines after each character. I would like to replace them with a single newline. I know they are newlines (hex code 0A), but the replaceAll() method for the String class isn't working.
Any help out there?
Jason
int arraySize = inventoryPages.length;
for (int i=0; i<arraySize; i++) {
String page = inventoryPages[i];
//System.out.print(page);
// Kill the blank lines in the string and replace them with one newline
page.replaceAll("\\u000A","|");
page.replaceAll("||","\\u000A");
System.out.print(page);
// Split the string into a List delimited by a \n (one element per line)
List lines = Arrays.asList(page.split("\n"));
// Get the initial size of the list
int listSize = lines.size();
// remove the last 15 lines of cruft, then the first line
for (int j = listSize; j>=listSize-14; j--) {
lines.remove(j);
}
lines.remove(0);
// Keep removing line 14 until it deletes what used to be line 27
for (int j= 14; j<=27; j++) {
lines.remove(14);
}
// Delete line 70 - 83
for (int j=70;j<=83; j++) {
lines.remove(70);
}
// Delete line 83 - The end
listSize = lines.size();
for (int j=83; j<listSize; j++) {
lines.remove(83);
}
// Create a new StringBuffer object, then append the remaining lines and
// the \n delimeter back to it
StringBuffer tempSB = new StringBuffer();
for (int j=0; j<lines.size(); j++) {
tempSB.append((String) lines.get(j)).append("\n");
}
// Update the information in the element
inventoryPages[i] = tempSB.toString();
}