输入不会 trim
Input won't trim
这是全新的,但我不确定我做错了什么,因为输出不会 trim。
package cs520.hw3.part1;
import javax.swing.JOptionPane;
public class StringTest {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter data using the format Name, Age, City");
//String delimiter = ",";
//String[] tokens = input.split(delimiter);
System.out.println(input.trim());
}
}
trim()
方法 returns 删除前导和尾随白色 space 的字符串副本,或者相同的字符串,如果它没有前导或尾随白色 space.
示例:
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String(" Some string with whitespace in front and after it. ");
System.out.print("Return Value :" );
System.out.println(Str.trim() );
}
}
输出:
Return Value :Some string with whitespace in front and after it.
删除前导和尾随空格(使用 trim()
和替换 String
中的 all 空格(使用 replaceAll(" ", "")
)
示例输出
' Andrew, 52, Sydney '
'Andrew, 52, Sydney'
'Andrew,52,Sydney'
- 原始字符串
trim()
replaceAll(" ", "")
代码
import javax.swing.*;
public class TrimSpace {
TrimSpace() {
String s = JOptionPane.showInputDialog(null,
"Name, Age, City",
"Person Details",
JOptionPane.QUESTION_MESSAGE);
// show the raw string entered by user
System.out.println(String.format("'%1s'", s));
// remove leading & trailing space from string
System.out.println(String.format("'%1s'", s.trim()));
// remove all spaces from string
System.out.println(String.format("'%1s'", s.replaceAll(" ", "")));
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new TrimSpace();
}
};
SwingUtilities.invokeLater(r);
}
}
这是全新的,但我不确定我做错了什么,因为输出不会 trim。
package cs520.hw3.part1;
import javax.swing.JOptionPane;
public class StringTest {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter data using the format Name, Age, City");
//String delimiter = ",";
//String[] tokens = input.split(delimiter);
System.out.println(input.trim());
}
}
trim()
方法 returns 删除前导和尾随白色 space 的字符串副本,或者相同的字符串,如果它没有前导或尾随白色 space.
示例:
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String(" Some string with whitespace in front and after it. ");
System.out.print("Return Value :" );
System.out.println(Str.trim() );
}
}
输出:
Return Value :Some string with whitespace in front and after it.
删除前导和尾随空格(使用 trim()
和替换 String
中的 all 空格(使用 replaceAll(" ", "")
)
示例输出
' Andrew, 52, Sydney '
'Andrew, 52, Sydney'
'Andrew,52,Sydney'
- 原始字符串
trim()
replaceAll(" ", "")
代码
import javax.swing.*;
public class TrimSpace {
TrimSpace() {
String s = JOptionPane.showInputDialog(null,
"Name, Age, City",
"Person Details",
JOptionPane.QUESTION_MESSAGE);
// show the raw string entered by user
System.out.println(String.format("'%1s'", s));
// remove leading & trailing space from string
System.out.println(String.format("'%1s'", s.trim()));
// remove all spaces from string
System.out.println(String.format("'%1s'", s.replaceAll(" ", "")));
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new TrimSpace();
}
};
SwingUtilities.invokeLater(r);
}
}