Replace strings with incrementing value - java

I have an ArrayList that is built from posts in a forum, each entry is a post.
When I am building the entry I am replacing all elements with the string "Image[x]".
After the array list is filled I want to go back and replace all instances of "[x]" with an incrementing integer.
I am building my array list using this code:
Elements wholePosts = doc.select("div.post_body");
for (Element wholePost : wholePosts) {
Elements texts = wholePost.select("div[itemprop=commentText]");
for (Element text : texts) {
String nobr = text.html().replaceAll("(?i)<br[^>]*>", "newlineplaceholder");
String formatted1 = nobr.replaceAll("<img src=.*?>", "Image[x]");
Document finalPost = Jsoup.parse(formatted1);
String almostfinalText = finalPost.text();
String finalText = almostfinalText.replace("newlineplaceholder", "\n");
datumList.add(finalText);
}
I have tried to replace the string and increment it in the above code but it only increments for each post element, so if there are multiple images in a post, post 1 would contain "Image1, Image1" and post 2 would contain "Image2, Image2". What I am looking for is for post 1 to contain "Image1, Image2" and post 2 to contain "Image3, Image4"

Related

I am new to selenium.How should i check a list of webelements(which are numbers) are masked after the 4th character

How can I use selenium java to verify that the data table are masked after the 4th character.
I have used the below code to extract the WebElements from the GUI.
List IDds = driver.findElements(By.xpath("//tbody/tr/td[1]"));
Example of data:
1111$$$$
2222$$
Find all the elements you need in a loop, or give a CSS expression that returns a list of web elements:
WebElement myElm = driver.findElementByCSS("CSSExpression");
String text = myElm.getAttribute("Text");
// or
// String text = myElm.getAttribute("Value");
Char C = text.charAt(4);
if (C=='$')
// Good
else
// bad
You can also find the first location in the string where you see $ and make sure all the other chars are the same.

Selenium-Get list index number based on text match

Automation scenario is to get all the links in weblist & then get this arraylist content outside loop and then get the index number dynamically based on the url hit.
String values="";
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) {
String temp = values;
values = temp + we.getText();
}
System.out.println("Text "+values);
int ind=values.indexOf("www.test.com");
System.out.println("Index "+ind);
The above code returns me a weird index number of 74.
The url_link output contents are:
wwww.hatch.com
wwww.tist.com
wwww.helix.com
wwww.patching.com
wwww.seyh.com
wwww.test.com
wwww.toast.com
wwww.telling.com
wwww.uity.com
so based upon the expected result i am expecting the index number to be 5.
Any help will be appreciated.
The variable values is a variable of type String, it's not a List. So when you do values = temp + we.getText();, you are basically appending all the links in a single String. So the output will actually be this:
wwww.hatch.comwwww.tist.comwwww.helix.comwwww.patching.comwwww.seyh.comwwww.test.comwwww.toast.comwwww.telling.comwwww.uity.com
The index of www.test.com is 74 in this string. You should be adding these links in an ArrayList and then find the index of the link you want to search.
Something like this should work:
List<String> values = new ArrayList<String>();
List<WebElement> url_link = driver.findElements(By.cssSelector(".anomaly>a"));
for ( WebElement we: url_link) {
values.add(we.getText());
}
int ind = values.indexOf("www.test.com");
System.out.println("Index "+ind);

How to convert WebElement to String or obtain String array?

I have a code that retrieves the URL's of a webpage. I wanted to know if it is possible to convert this to a String or obtain a String from the System.out.print() method?
for(WebElement link : links)
System.out.println(link.getAttribute("href"));
Edit1: I would like to create an array of strings obtained from the above code if it is possible.
I would like to copy the codes output information and use it in an email for only 15 items:
for(int x = 1; x < 15 ; x++) {
String url = link.getAttribute("href");
messageBodyPart.setText(url);}
If you want to have an array list you can have it as:
List<String> urls = new ArrayList<String>();
for(WebElement link : links){
urls.add(link.getAttribute("href"))
}
And then use it by fetching list elements in your java mail.

Jsoup - How can i get all links and titles in one array?

this is my first question so far. I need to get links and titles from certain html page in one 2D Array. Here is my code:
public String[][] data;
descs = doc.select("a");
data= new String [spaceCount][2];
int count=0;
for (Element e : descs ) {
data[count][0]=descs.attr("href");
data[count][1]=descs.attr("title");
count++;
}
String svalues = data[0][0]+"\n"+data[0][1]+data[1][0]+"\n"+data[1][1];
output.setText(svalues);
But my problem is that it keeps getting the same data in every place. I mean that in every cell here is only one, same link and one, same title. I am newbie in java, but I think things in loop are not moving (and they should). Can anyone explain how to make it work?
You are not using Element e. Change
data[count][0]=descs.attr("href");
data[count][1]=descs.attr("title");
to
data[count][0]=e.attr("href");
data[count][1]=e.attr("title");
and add as last line of the for loop:
if ( count == spaceCount )
break;

To ge the part of the String and Compare witha a variable(String Variable)

I have the String like:
String value = "13,14,15,16,17"
But i Dont know how many numbers are there with comma separation.
I want to compare with the variable say:
String varValue = "16"
It may be in any postion..
I want to compare these two string variables....
Please can anyone help?
You can do this sort of thing:
String values = '13,14,15,16,17'
String required = '16'
values.tokenize( ',' ).with { toks ->
println "There are ${toks.size()} elements in the list"
println "The list contains $required is ${toks.contains( required )}"
println "It is at position ${toks.indexOf( required )}"
}
Which prints
There are 5 elements in the list
The list contains 16 is true
It is at position 3
Use the split method to put the numbers in an array and then compare.
Here: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
Do not quite understand your question.
Dont know how many numbers are there with comma separation
use String method split()
I want to compare with the variable say: String varValue = "16"
use String method contains()
You can use the split method to receive an Array. Turning the Array into a List will allow you to use some helper functions such as contains and indexOf, which can be used to return whether the token exists or the position of the token.
String value = "13,14,15,16,17";
//Checks existence
boolean contains = Arrays.asList(value.split(",")).contains("16");
//Returns position
int pos = Arrays.asList(value.split(",")).indexOf("16") + value.split(",").length + 1;
These examples all use Java.
If you are using Java , then following can be done to achieve this :
1. Split the input string into an array.
2. convert that array into a list .
Now
a) To find total elements separated by comma , Use :
size().
b) To find if the list contains required element or not , Use :
contains().
c) To find position of element in list , Use :
indexof()
So the code will look like :
import java.util.Arrays;
import java.util.List;
public class Test{
public static void main(String[] args) {
String inputString = "13,14,15,16,17";
String element = "16";
// Convert the string into array.
String values[] = inputString.split(",");
// Create a list using array elements.
List<String> valList = Arrays.asList(values);
System.out.println("Size :" + valList.size());
System.out.println("List contains 5 " + valList.contains(element));
System.out.println("Position of element" + valList.indexOf(element));
}
}

Categories