Replacing Newlines?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Replacing Newlines?

  1. #1
    Join Date
    Jun 2005
    Posts
    4

    Question Replacing Newlines?

    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:

    Code:
    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


    Code:
            
            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();
    
            }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The page.replaceAll("\\u000A","|"); will return a String with the replacements,
    so do like:

    page=page.replaceAll("\\u000A","|");
    page=page.replaceAll("||","\\u000A");

    Strings are immutable...
    Last edited by sjalle; 06-08-2005 at 11:21 AM.
    eschew obfuscation

  3. #3
    Join Date
    Jun 2005
    Posts
    4
    Argh... the stupid errors are the hardest to find.

    Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center