如何从ArrayList <string>中删除所有标点符号?含义&#34;,&#34;和&#34;。&#34;

时间:2018-03-11 20:42:36

标签: selenium arraylist punctuation

我有一个ArrayList,其中包含一个字符串形式的数字,它们取自WebElement列表(复制到其中)。

我有2个,我想比较它们,但因为它们有一些&#34;,&#34;和&#34;。&#34;在这些数字中,我很难做到这一点。

我试图研究类似的问题 How can I remove punctuation from input text in Java?

但未能在我的arrayList上实现它。

  

代码中的列表可以很容易地找到:
  / / *****列表#1 - 需要忽略所有非数字字符***** ///   和       *****列表#1 - 需要忽略所有非数字字符***** ///

public static void WatchlistInstrumentsList(WebDriver driver, boolean finalstatus) throws InterruptedException
{

    List<Integer> memoryOfSelectedi = new ArrayList<Integer>(); // Remembering the 'Clock = Green' instruments


    int size = 2;
    int forCounter1=0;

    for (int i = 1; i < size ; i++) {
        forCounter1=i;

        // The selector of "Last" price = Streamer
        listOfLastPrice1= driver.findElements(By.cssSelector("[data-column-name='last'][class*='pid']")); 





// ***** LIST #1 - NEED TO IGNORE ALL NON_NUMERIC CHARACTERS *****///
    ArrayList<String> listCopyLastPrice1 = new ArrayList<String>();

    for (WebElement element : listOfLastPrice1) {
        listCopyLastPrice1.add(element.getText());
    }


    System.out.println("Number of Open stocks out of the list is: " +memoryOfSelectedi.size());



    WatchlistCheckSocket(driver, listCopyLastPrice1,  memoryOfSelectedi,listOfLastPrice1, forCounter1) ;
    finalstatus = true;
}






public static void WatchlistCheckSocket(WebDriver driver,List listCopyLastPrice1,
        List<Integer> memoryOfSelectedi,  List<WebElement> listOfLastPrice1, int forCounter1) throws InterruptedException
         {



    // ******** CHANGE LATTER TO 180000 ******///
    Thread.sleep(60000);


    List<WebElement> listOfLastPrice2=null;
    int size = 2;
    int forCounter2;


    for (int z = 0; z < size ; z++) {

        listOfLastPrice2= driver.findElements(By.cssSelector("[data-column-name='last'][class*='pid']")); 
        size = listOfLastPrice2.size();
        String NewPrice = listOfLastPrice2.get(z).getText();
        System.out.println("Last Price is: " +listOfLastPrice2.get(z).getText());

    }



// LIST #2 - NEED TO IGNORE ALL NON_NUMERIC CHARACTERS ///
    // Put all Values of WebElement listOfLastPrice2 List, into a String array list //
    ArrayList<String> listCopyLastPrice2 = new ArrayList<String>();

    for (WebElement element : listOfLastPrice2) {
        listCopyLastPrice2.add(element.getText());
    }




    // ****** COMPARE GREEN INSTRUMENTS, LIST1_Price VS LIST2_Price ****//
    int sizeList=0;
    while (sizeList<memoryOfSelectedi.size())
    {
        if (listCopyLastPrice1.get(memoryOfSelectedi.get(sizeList)) != listOfLastPrice2.get(memoryOfSelectedi.get(sizeList)).getText())
        {

            System.out.println("First price was: " +listCopyLastPrice1.get(memoryOfSelectedi.get(sizeList)));
            System.out.println("Second price was: " +listCopyLastPrice2.get(memoryOfSelectedi.get(sizeList)));


        }
        sizeList++;

    }}}

1 个答案:

答案 0 :(得分:0)

如果我将列表的语法修改为,那会有帮助吗?

 listCopyLastPrice2.add(element.getText().replaceAll("[^A-Za-z0-9]", ""));