如何删除字符串中多余的换行符

how to delete up extra line breakers in string

我的 String 中有这样的文本(我已经从 txt.file 中读到)

 trump;Donald Trump;trump@yahoo.eu    
 obama;Barack Obama;obama@google.com   
 bush;George Bush;bush@inbox.com    
 clinton,Bill Clinton;clinton@mail.com

然后我试图切断除电子邮件地址之外的所有内容并在控制台上打印出来

String f1[] = null;
f1=s.split("(.*?);");
for (int i=0;i<f1.length;i++) {
       System.out.print(f1[i]);
   }

我的输出是这样的:

trump@yahoo.eu  
obama@google.com   
bush@inbox.com  
clinton@mail.com

如何避免这样的输出,我的意思是如何在没有换行符的情况下获得输出文本?

尝试使用以下方法。我已经用 ScannerBufferedReader 阅读了你的文件,在这两种情况下,我都没有换行。 file.txt是包含文本的文件,拆分逻辑和你一样

public class CC {
public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(new File("file.txt"));

    while (scan.hasNext()) {
        String f1[] = null;
        f1 = scan.nextLine().split("(.*?);");
        for (int i = 0; i < f1.length; i++) {
            System.out.print(f1[i]);
        }
    }
    scan.close();

    BufferedReader br = new BufferedReader(new FileReader(new File("file.txt")));
    String str = null;
    while ((str = br.readLine()) != null) {
        String f1[] = null;
        f1 = str.split("(.*?);");
        for (int i = 0; i < f1.length; i++) {
            System.out.print(f1[i]);
        }
    }
    br.close();
}
}

您可以按照以下代码所示替换所有断行符:

String f1[] = null;
f1=s.split("(.*?);");
for (int i=0;i<f1.length;i++) {
    System.out.print(f1[i].replaceAll("\r", "").replaceAll("\n", ""));
}

这将替换所有没有 space 的人。

package com.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {
        String s = "trump;Donald Trump;trump@yahoo.eu    "
                + "obama;Barack Obama;obama@google.com   "
                + "bush;George Bush;bush@inbox.com    "
                + "clinton;Bill Clinton;clinton@mail.com";

        String spaceStrings[] = s.split("[\s,;]+");
        String output="";
        for(String word:spaceStrings){
            if(validate(word)){
                output+=word;
            }
        }
        System.out.println(output);
    }

    public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile(
            "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$",
            Pattern.CASE_INSENSITIVE);

    public static boolean validate(String emailStr) {
        Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
        return matcher.find();
    }

}

您可以通过使用否定字符 class [^\s;]+ 后跟 @ 并再次匹配一次或多次不匹配分号或空格字符来匹配类似电子邮件的格式,而不是拆分不是分号或空白字符。

final String regex = "[^\s;]+@[^\s;]+";
final String string = "trump;Donald Trump;trump@yahoo.eu    \n"
         + " obama;Barack Obama;obama@google.com   \n"
         + " bush;George Bush;bush@inbox.com    \n"
         + " clinton,Bill Clinton;clinton@mail.com";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final List<String> matches = new ArrayList<String>();
while (matcher.find()) {
    matches.add(matcher.group());
}
System.out.println(String.join("", matches));

[^\s;]+@[^\s;]+

Regex demo

Java demo

只需替换可能到达开始和结束的'\n'即可。 这样写。

String f1[] = null;
f1=s.split("(.*?);");
for (int i=0;i<f1.length;i++) {
f1[i] = f1[i].replace("\n");
System.out.print(f1[i]);
}