My String is
**abcd
*[abc]
<td> **Welcome
**Welcome Again
</td>
Is their any way in which I can remove the * symbol in between the tags so that my final string string would be something like
**abcd
*[abc]
<td> Welcome
Welcome Again
</td>
Here all the * between <td> and </td> are removed
I dont want to use string.
Try this,
if (s.contains("<td>"))
{
String first = s.substring(0, s.indexOf("<td>"));
String last = s.substring(s.indexOf("<td>"), s.indexOf("</td>") + 5);
System.out.println("result : "+first + last.replace("**", ""));
}
else
{
System.out.println("result : "+s);
}
Related
I am attempting to build a world clock, to display GMT, PST etc.
The only problem I'm having is that when the user types nothing in the text field, it's not going into the if statement that only executes when the user doesn't enter anything and displays the local time. I have provided all my code to see what the problem is.
html code:
<body>
<form action="WorldClockWebApp" method="post">
<table>
<tr>
<td>Enter Time Zone:</td>
<td><input type="text" name="timezone" /></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
servlet code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String input = request.getParameter("timezone");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
ZoneId z;
boolean valid = true;
TimeZone t = null;
String[] ids = TimeZone.getAvailableIDs();
if (input != "") // If the user enters in a timezone.
{
for (String id : ids) {
if (input.equalsIgnoreCase(id))
t = TimeZone.getTimeZone(id);
}
}
if (t == null)
response.getWriter().append("<h1>What's the time Mr Wolf?</h1>")
.append("<p>No time provided, but the local time is " + t.getDisplayName().toString() + " is:</p>")
.append("<h2>" + LocalTime.now(t.toZoneId()).format(dtf) + "</h2>")
.append("<form action=\"/project4/WorldClock.html\">" + "<button type=\"submit\">Go Back</button>"
+ "</form>"); // Button gives user option to go back.
else
response.getWriter().append("<h1>What's the time Mr Wolf?</h1>")
.append("<p>The current time in " + t.getDisplayName().toString() + " is:</p>")
.append("<h2>" + LocalTime.now(t.toZoneId()).format(dtf) + "</h2>")
.append("<form action=\"/project4/WorldClock.html\">" + "<button type=\"submit\">Go Back</button>"
+ "</form>"); // Button gives user option to go back.
}
I think you need to do if input != null instead of if input !="".
assuming that you implement doPost method and it is like doGet method, you should use equals instead of != "" .
your code can be like below code.
if (!input.equals(""))
I am using jsoup in my android app to parse my html code but now I need parse table data and I can not get it to work. I try many ways but not successful so I want try luck here if anyone have experience.
Here is part of my html:
<div id="editacia_jedla">
<h2>My header</h2>
<h3>My sub header</h3>
<table border="0" class="jedalny_listok_tabulka" cellpadding="2" cellspacing="1">
<tr>
<td width="100" class="menu_nazov neparna" align="left">Food Menu 1</td>
<td class="jedlo neparna" align="left">vegetable and beef
<div class="jedlo_box_alergeny">Allergens: 1, 3</div>
</td>
</tr>
<tr>
<td width="100" class="menu_nazov parna" align="left">Food Menu 2</td>
<td class="jedlo parna" align="left">Potato salad and pork
<div class="jedlo_box_alergeny">Allergens: 6</div>
</td>
</tr>
</table>
etc
</div>
My java/android code:
try {
String tableHtmlCode="";
Document fullHtmlDocument = Jsoup.connect(urlOfFoodDay).get();
Element elm1 = fullHtmlDocument.select("#editacia_jedla").first();
for( Element element : elm1.children() )
{
tableHtmlCode+=element.getElementsByIndexEquals(2); //this set table content because 0=h2, 1=h3
}
Document parsedTableDocument = Jsoup.parse(tableHtmlCode);
//Element th = parsedTableDocument.select("td[class=jedlo neparna]").first(); THIS IS BAD
String foodContent="";
String foodAllergens="";
}
So now I want extract text vegetable and beef and save it to string foodContent and numbera 1, 3(together) from div class jedlo_box_alergeny save to string foodAllergens. Someone can help? I will very grateful for any ideas
Iterate over your document's parent tag jedalny_listok_tabulka and loop over td tags.
td tag is the parent to href tags which include the allergy values. Hence, you would loop over the tags a elements to get your numbers, something like:
Elements myElements = doc.getElementsByClass("jedalny_listok_tabulka")
.first().getElementsByTag("td");
for (Element element : myElements) {
if (element.className().contains("jedlo")) {
String foodContent = element.ownText();
String foodAllergen = "";
for (Element href : element.getElementsByTag("a")) {
foodAllergen += " " + href.text();
}
System.out.println(foodContent + " : " + foodAllergen);
}
}
Output:
vegetable and beef : 1 3
Potato salad and pork : 6
I have a string:
Am trying to do the below :
int i =0;
for(String s : appFields){
i++;
String divid = "chart_"+i;
divid = divid.replaceAll("[\\r\\n]+$", "");
}
I would like to trim it so that the value is only chart_1 and so on.
Can someone help me please?
<%
String[] appFields = "Account Information,Action Status,Activity Name,Activity Status,Last Activity Timestamp,Geographical Region,Enterprise Status,Business Process,Numer of Pages,Message Direction".split(",");
int i =0;
for(String s : appFields){
i++;
String divid = "chart_"+i;
divid = divid.replaceAll("[\\r\\n]+$", "");
%>
<tr>
<td><% out.println(i); %></td>
<td><% out.println(s); %></td>
<td class='with-3d-shadow with-transitions'><p><svg id="<% out.println(divid); %>" class="sparkline"></svg></p></td>
</tr>
<%
}
%>
This is the output i get in Chrome Web Inspector
<td class="with-3d-shadow with-transitions"><p><svg id="chart_1
" class="sparkline"></svg></p></td>
temp = temp.trim();
Strings are immutable so operations return new Strings that you have to assign back to the original reference variable.
Change your line to
<svg id="<%out.print(divid);%> " class="sparkline"></svg>
In other words, use print instead of println. println adds a new line after your String.
Just playing around and pulling some data off a site to manipulate when I come across this:
String request = "http://foo";
String data = "bar";
Connection.Response res = Jsoup.connect(request).data(data).method(Method.POST).execute();
Document doc = res.parse();
Elements all = doc.select("td");
for(Element elem : all){
String test = elem.text();
if(test.equals(" ")){
//redefine test to 0 and print it
}
else{
//print it
}
The site in question is coded as so:
<td align="center">Henry</td>
<td>23</td>
<td align="center">Savannah</td>
<td>15</td></tr>
...
<td align="center"> </td>
<td> </td>
<td align="center">Jane</td>
<td>15</td></tr>
In my for loop, test is never redefined.
I've debugged in Eclipse and String test is showing as so:
Edit
Debugging test chartAt(0):
org.jsoup.nodes.Element.text() says "Returns unencoded text or empty string if none". I'm assuming the unencoded part has something to do with this, but I can't figure it out.
I ran a test program:
public static void main(String[] args) {
String str = " ";
if (str.equals(" ")){
System.out.println("True");
}
}
and it returns true.
What gives?
I don't know if you control the HTML being sent in the body of the response or if that is what you see in a browser's source page or elsewhere
<td> </td>
But it's possible the actual content is
<td> </td> // or  
where   is the HTML entity for the non-breaking space.
In java, you can represent it as
char nbsp = 160;
So you could just check for both char values, the one for space and the one for non-breaking space.
Note that there might be other codepoints that are represented as white space. You need to know what you're looking for.
How can i remove the last <br> from a string with replace() or replaceAll()
the <br> comes after either a <br> or a word
my idea is to add a string to the end of a string and then it'll be <br> +my added string
how can i replace it then?
Regexp is probably not the best for this kind of task. also check answers to this similar question.Looking for <br> you could also find <BR> or <br />
String str = "ab <br> cd <br> 12";
String res = str.replaceAll( "^(.*)<br>(.*)$", "$1$2" );
// res = "ab <br> cd 12"
If you are trying to replace the last <br /> which might not be the last thing in the string, you could use something like this.
String replaceString = "<br />";
String str = "fdasfjlkds <br /> fdasfds <br /> dfasfads";
int ind = str.lastIndexOf(replaceString);
String newString = str.substring(0, ind - 1)
+ str.substring(ind + replaceString.length());
System.out.println(newString);
Output
fdasfjlkds <br /> fdasfds> dfasfads
Ofcourse, you'll have to add some checks to to avoid NPE.
Not using replace, but does what you want without a regex:
String s = "blah blah <br>";
if (s.endsWith("<br>")) {
s = s.substring(0, s.length() - 4);
}
Using regex, it would be:
theString.replaceAll("<br>$", "");