从String中删除某些文本

时间:2017-01-31 12:25:08

标签: java string email file-io postmark

所以我一直在编写一个程序来使用邮戳通过GUI发送电子邮件。我的问题是什么,它有效,但我有一个SubscribedList和一个unsubbedList。

我想要实现的是扫描subbed并取消选中,然后发送包含从subbed中删除的unsubbed的电子邮件字符串并将其发送到另一个变量。我已经通过以下方式实现了这一目标:

public static void getEmail() throws Exception{

File Emailpath = new File(EmailListPath);
File Unsubpath = new File("c:\\test\\PostMark\\unsubbedEmailList.txt");
FileInputStream fis = new FileInputStream(Emailpath);
InputStreamReader isr = new InputStreamReader(fis);

FileInputStream unsubInput = new FileInputStream(Unsubpath);
InputStreamReader unsubisr = new InputStreamReader(unsubInput);

Scanner readEmail = new Scanner(Emailpath);
Scanner readUnsub = new Scanner(Unsubpath);
int i = 0;
System.err.println("Subbed List: ");
while (readEmail.hasNext()) {
    //i++;
    String line = readEmail.nextLine();
    System.err.println(line);
    EmailAddress = EmailAddress+","+line; 
}
System.err.println("\nUnsubbed List: ");
while (readUnsub.hasNext()) {
    //i++;
    String line = readUnsub.nextLine();
    System.err.println(line);
    unsubbedEmailAddress = unsubbedEmailAddress+","+line; 
}
//System.err.println("\n"+EmailAddress);
unsubbedEmailAddress=unsubbedEmailAddress.substring(1, unsubbedEmailAddress.length());
EmailAddress=EmailAddress.substring(1, EmailAddress.length());
EmailAddress = EmailAddress.replaceAll(unsubbedEmailAddress,"");
System.err.println("Subbed:\n"+EmailAddress);
System.err.println("Unsubbed:\n"+unsubbedEmailAddress);
System.err.println("\nSending to:\n"+EmailAddress);

最后几行是我想要的,但是未提取的必须按特定的顺序正确地从底层字符串中删除。

所以我在想这样的事情?相反,字符串将从文件读入,但基本上做同样的工作......

String subbed = "aaronjoyce2@gmail.com,aaronjoyce09@gmail.com,aaronscallyjoyce@gmail.com",
                unsubbed = "aaronjoyce09@gmail.com";
        String[] subbedArray, unsubbedArray, sendArray = null;

    subbedArray = subbed.split(",");
    for(int i = 0;i<=subbedArray.length -1;i++){
        //System.err.println("Subbed:");
        System.out.println(subbedArray[i]);
    }   
    System.err.println("\n");
    unsubbedArray=unsubbed.split(",");
    for(int i = 0;i<=unsubbedArray.length-1;i++){

        System.out.println(unsubbedArray[i]);
    }
    System.err.println("\n");
    for(int i = 0;i<=unsubbedArray.length-1;i++){
        int j=0;
        if(subbedArray[i].equals(unsubbedArray[j]))
            sendArray[i]= unsubbedArray[i];
        else
            for(j = 0;j<=unsubbedArray.length-1;j++){
                if(subbedArray[i].equals(unsubbedArray[j]))
                    sendArray[i]= unsubbedArray[i];
            }
        System.out.println(sendArray[i]);
    }

1 个答案:

答案 0 :(得分:0)

String subbed = "aaronjoyce2@gmail.com,aaronjoyce09@gmail.com,aaronscallyjoyce@gmail.com",
           unsubbed = "aaronjoyce09@gmail.com";
    ArrayList<String> subbedArray=new ArrayList<String>();
    ArrayList<String> unsubbedArray=new ArrayList<String>();
    ArrayList<String> sendArray=new ArrayList<String>();

    subbedArray = Arrays.asList(subbed.split(","));
    unsubbedArray =Arrays.asList(unsubbed.split(","));
    for(int i = 0;i<unsubbedArray.size();i++){
      for(int j=0;j<subbedArray.size();j++){
          if(unsubbedArray.get(i).equals(subbedArray.get(j))){
            subbedArray.remove(j);
          }
       }
    }
    sendArray=subbedArray;

这可能会奏效,但我无法测试它。把它作为一个想法并与​​之合作