(更新的问题)
首先,我认为"\n"
等同于System.getProperty("line.separator")
我编写了一些用于处理字符串的方法,其中一些方法检查是否存在换行符。
if (string.charAt(i) == '\n') {//do something;}
但是我注意到检查"\n"
并不匹配使用System.getProperty("line.separator")
添加的换行符。
这是一个SSCCE,以证明我的说法!:
描述:
两个完全相同的文本字符串;一个使用"\n"
添加了新行,另一个使用System.getProperty("line.separator")
添加了新行。
有一个名为String removeExtraNewLines(String)
的方法,用于删除字符串中的任何额外换行符并将其返回;正如其标题所示。使用此方法过滤的两个字符串。
两个按钮buttonAlpha
和buttonBeta
分别使用过滤后的字符串设置JTextArea
的文本
您会注意到,该方法捕获/匹配并删除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'
?这不需要更多的复杂对比即可发现,对吧? - BlackVegetableif (string.charAt(i) == System.getProperty("line.separator"))
是否与您的预期不符?第三,您是否尝试使用调试器来检查System.getProperty("line.separator")
的实际值是什么? - Jesse Webb\n
是平台相关的,System.getProperty("line.separator")
可能是\n
或\r\n
。 - assylias