Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Why checking for "\n" doesn't match new lines added using System.getProperty("line.separator")
    primarykey
    data
    text
    <p>(<strong>Updated Question</strong>)</p> <p>First, I think that <code>"\n"</code> is equivalent to <code>System.getProperty("line.separator")</code></p> <p>I wrote some methods to work with Strings some of them check for existence of new line</p> <p><code>if (string.charAt(i) == '\n') {//do something;}</code></p> <p>But I noticed that checking for <code>"\n"</code> doesn't match the new lines added by the <code>System.getProperty("line.separator")</code></p> <p>This is an <strong>SSCCE</strong> to demonstrate my claim!:</p> <p><strong>Description:</strong><br> Two strings of identical text; one <code>alpha_String</code> has new lines added using <code>"\n"</code>, and the other <code>beta_String</code> has new lines added using <code>System.getProperty("line.separator")</code></p> <p>There is a method named <code>String removeExtraNewLines(String)</code> used to remove any extra new lines in a String and return it back; as its header suggests. The two strings filtered using this method.</p> <p>The two buttons <code>buttonAlpha</code> and <code>buttonBeta</code> each set the text of the <code>JTextArea</code> with the filtered String</p> <p><strong>You will notice</strong> that the the method catch/match and remove extra new lines of <code>alpha_String</code> but don't do the same with <code>beta_String</code></p> <pre><code>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 &lt; myNewString.length(); i++) { if (i &gt; 1) { previouseChar = myNewString.charAt(i - 1); } if ((myNewString.charAt(i) == '\n') &amp;&amp; (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(); } }); } } </code></pre> <p><strong>So the question</strong>: Why checking for <code>"\n"</code> doesn't match new lines added using <code>System.getProperty("line.separator")</code>?, And How to match them?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload