2013-01-23 8 views
8

(Aktualizacja Pytanie)Java: Dlaczego sprawdzanie " n" nie pasuje do nowych linii dodane za pomocą System.getProperty ("line.separator")

Po pierwsze, myślę, że "\n" jest równoważna System.getProperty("line.separator")

pisałem kilka metod do pracy z ciągami niektóre z nich sprawdzić istnienie nowej linii

if (string.charAt(i) == '\n') {//do something;}

Ale zauważyłem, że sprawdzanie "\n" nie pasuje do nowych linii dodanych przez System.getProperty("line.separator")

To SSCCE zademonstrować moje roszczenie !:

Opis:
Dwa ciągi identycznego tekstu; jeden alpha_String ma nowe linie dodane za pomocą "\n", a pozostałe beta_String ma nowe linie dodane za pomocą System.getProperty("line.separator")

Jest to metoda o nazwie String removeExtraNewLines(String) stosowany w celu usunięcia wszelkich dodatkowych nowych linii w ciągu znaków i odesłać go z powrotem; jak sugeruje jego nagłówek. Dwa ciągi filtrowane przy użyciu tej metody.

Dwa przyciski buttonAlpha i buttonBeta Każdy zestaw tekst JTextArea z przefiltrowanej String

Zauważysz że metoda połowu/mecz i usunąć dodatkowe nowe linie alpha_String ale nie zrobić samo z beta_String

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

public class NewLineTest extends JPanel 
{ 

    JPanel buttonPanel; 
    JPanel textAreaPanel; 
    JButton buttonAlpha; 
    JButton buttonBeta; 
    JTextArea textArea; 
    String n = "\n"; 
    String s = System.getProperty("line.separator"); 
    String alpha_String; 
    String beta_String; 

    public NewLineTest() 
    { 
     createSentencesText(); 
     buttonAlpha = new JButton("Alpha String"); 
     buttonAlpha.addActionListener(eventWatcher); 

     buttonBeta = new JButton("Beta String"); 
     buttonBeta.addActionListener(eventWatcher); 

     textArea = new JTextArea(0, 0); 
     JScrollPane scrollTextArea = new JScrollPane(textArea); 
     scrollTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     textArea.setEditable(false); 

     buttonPanel = new JPanel(); 
     textAreaPanel = new JPanel(new BorderLayout()); 

     buttonPanel.add(buttonAlpha); 
     buttonPanel.add(buttonBeta); 

     textAreaPanel.add(scrollTextArea, BorderLayout.CENTER); 

     JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, textAreaPanel, buttonPanel); 
     splitPane.setDividerLocation(400); 
     splitPane.setResizeWeight(.5d); 
     this.setLayout(new BorderLayout()); 
     this.add(splitPane); 
    } 

    private void createSentencesText() 
    { 
     alpha_String = "A: Let’s go to the beach." + n + n 
       + "B: That’s a great idea." + n 
       + "A: We haven’t been in a while." + n + n 
       + "B: We haven’t been in a month." + n 
       + "A: The last time we went, you almost drowned." + n 
       + "B: No, I didn’t." + n + n + n 
       + "A: Then why did the lifeguard dive into the water?" + n 
       + "B: I think he wanted to cool off." + n 
       + "A: He swam right up to you." + n 
       + "B: And then he turned right around." + n 
       + "A: Maybe you’re right." + n 
       + "B: Maybe we should get going."; 


     beta_String = "A: Let’s go to the beach." + s + s 
       + "B: That’s a great idea." + s 
       + "A: We haven’t been in a while." + s + s 
       + "B: We haven’t been in a month." + s 
       + "A: The last time we went, you almost drowned." + s 
       + "B: No, I didn’t." + s + s + s 
       + "A: Then why did the lifeguard dive into the water?" + s 
       + "B: I think he wanted to cool off." + s 
       + "A: He swam right up to you." + s 
       + "B: And then he turned right around." + s 
       + "A: Maybe you’re right." + s 
       + "B: Maybe we should get going."; 
    } 

    public static String removeExtraNewLines(String s) 
    { 
     String myNewString = s.trim(); 
     StringBuilder stringB = new StringBuilder(); 

     char previouseChar = '~'; 
     for (int i = 0; i < myNewString.length(); i++) 
     { 
      if (i > 1) 
      { 
       previouseChar = myNewString.charAt(i - 1); 
      } 
      if ((myNewString.charAt(i) == '\n') && (previouseChar == '\n')) 
      { 
       continue; 
      } 

      stringB.append(myNewString.charAt(i)); 
     } 
     myNewString = stringB.toString(); 
     return myNewString; 
    } 
    AbstractAction eventWatcher = new AbstractAction() 
    { 
     @Override 
     public void actionPerformed(ActionEvent ae) 
     { 
      Object source = ae.getSource(); 
      if (source == buttonAlpha) 
      { 
       String arranged_string_alpha = removeExtraNewLines(alpha_String); 
       textArea.setText(arranged_string_alpha); 
      } 
      if (source == buttonBeta) 
      { 
       String arranged_string_beta = removeExtraNewLines(beta_String); 
       textArea.setText(arranged_string_beta); 
      } 
     } 
    }; 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame("NewLine Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(700, 300); 
     frame.add(new NewLineTest(), BorderLayout.CENTER); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       UIManager.put("swing.boldMetal", Boolean.FALSE); 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

więc pytanie: Dlaczego sprawdzanie "\n" nie pasuje nowe linie dodane za pomocą System.getProperty("line.separator")? I jak je dopasować?

+1

Czy zweryfikowałeś, że w systemie stała systemowa jest w rzeczywistości równa '\ n''? Nie powinno to zająć więcej niż proste porównanie, prawda? – BlackVegetable

+1

Przede wszystkim, którego systemu operacyjnego używasz do testowania tego? Po drugie, czy 'if (string.charAt (i) == System.getProperty (" line.separator "))' nie działa zgodnie z oczekiwaniami? Po trzecie, czy próbowałeś użyć debuggera do sprawdzenia, czym właściwie jest wartość 'System.getProperty (" line.separator ")'? –

+0

Ten artykuł może Cię zainteresować http://en.wikipedia.org/wiki/Newline – Pshemo

Odpowiedz

20

Po pierwsze, myślę, że „\ n” jest równoznaczne z System.getProperty („line.separator”) oprócz tego, że później jest niezależna od platformy.

Nie, ta ostatnia to specyficzna dla platformy. Jest to niezależny od platformy sposób uzyskania separatora linii platformy.

Tak więc w systemie Windows oczekiwałbym, że System.getProperty("line.separator") zwróci "\ r \ n", na przykład.

Jeśli chodzi o to, co twój textArea używa do nowej linii - to całkowicie zależy od tego, co jest textArea - i nie dałeś nam żadnych informacji na ten temat.

+1

Tak finezyjna z językiem - a jednak tak poprawne. +1 – BlackVegetable

+9

@BlackVegetable: Pedantry jest ważną częścią bycia inżynierem oprogramowania :) –

+0

Zamierzam zacytować cię na ten temat teraz! – BlackVegetable

Powiązane problemy