简体   繁体   English

从字符串中删除某些文本

[英]Remove certain text from String

So I've been working on a programm to use postmark to send emails through a GUI. 因此,我一直在研究使用邮戳通过GUI发送电子邮件的程序。 What my issue is it works and all, but I have a SubscribedList and an unsubbedList. 我的问题是,它正常工作,但是我有一个SubscribedList和一个unsubbedList。

What I'm trying to achieve is to scan the subbed and unsubbed and then send a string of the emails containing the unsubbed removed from the subbed and send it to another variable. 我要达到的目的是扫描子床和未子床,然后发送一串包含从子床中删除的未子床的电子邮件,并将其发送到另一个变量。 I've half achieved this by: 我已经通过以下方式实现了这一目标:

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);

The last couple of lines half does what I want, but the unsubbed have to be in a specific order to properly remove from the subbed string. 最后两行的一半完成了我想要的操作,但是未字幕的顺序必须是特定顺序才能从字幕字符串中正确删除。

so I was thinking something like this? 所以我在想这样的事情? Instead the String would be read in from a file but basically do the same job... 取而代之的是从文件中读取String,但是基本上可以完成相同的工作...

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]);
    }
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;

this might work, but i cant test it. 这可能有效,但我无法对其进行测试。 take it as a idea and work with it 把它当作一个想法并与​​之合作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM