簡體   English   中英

Java:為什么檢查“\\ n”與使用System.getProperty(“line.separator”)添加的新行不匹配

[英]Java: Why checking for “\n” doesn't match new lines added using System.getProperty(“line.separator”)

更新的問題

首先,我認為"\\n"等同於System.getProperty("line.separator")

我寫了一些與Strings一起使用的方法,其中一些方法檢查是否存在新行

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

但是我注意到檢查"\\n"System.getProperty("line.separator")添加的新行不匹配

這是一個SSCCE來證明我的主張!:

描述:
兩串相同的文字; 一個alpha_String使用"\\n"添加新行,另一個beta_String使用System.getProperty("line.separator")添加新行

有一個名為String removeExtraNewLines(String)用於刪除String removeExtraNewLines(String)中的任何額外新行並將其返回; 正如其標題所示。 使用此方法過濾了兩個字符串。

兩個按鈕buttonAlphabuttonBeta每個設置的文本JTextArea與過濾的字符串

您會注意到方法捕獲/匹配並刪除額外的新行alpha_String但不會對alpha_String執行相同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();
            }
        });
    }
}

所以問題 :為什么檢查"\\n"與使用System.getProperty("line.separator")添加的新行不匹配?,以及如何匹配它們?

首先,我認為“\\ n”等同於System.getProperty(“line.separator”),除了后者與平台無關。

不,后者是特定於平台的 這是獲取平台線分隔符的獨立平台的方式。

所以在Windows上,我希望System.getProperty("line.separator")返回“\\ r \\ n”,例如。

現在關於textArea用於換行的內容 - 完全取決於textArea是什么 - 並且您沒有向我們提供任何相關信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM