This is a very basic question
I am using the Bing translate API method: Translate.execute(String to be translated,Target Language)
When there is no newline character in the source language then it is all fine. E.g.
String str = "I have seen some app. Educational and fun.";
But If my source text has multiple lines and looks like following, how do I create a String variable for it:
I have seen some app.
Educational and fun.
I don't want to add /n, /r characters inside my string because the bing API will try to translate these characters also.
Can you instead translate each sentence or line at a time and combine them after the fact?
String str1 = "I have seen some app.";
String str2 = "Educational and fun.";
String result = Translate.execute(str1) + "\n" + Translate.execute(str2);
Or translate it all at once and add the newlines characters in after you get the translation back? Maybe something like (may be too simplistic):
String str = "I have seen some app. Educational and fun.";
String result = Translate.execute(str);
result = result.replaceAll(".", "\n");
Related
I'm working on a program, which formats HTML Code, extracted from a PDF file.
I have a String list, which contains paragraphs and is divided by that.
As the PDF has hyperlinks, I decided to replace them with a foot note number "[1]".
This will be used for citation of sources. I will eventually plan, to put it at the end of a paragraph, or sentence, so you can look up the sources, like you would in a book.
My Problem
For some reason not all the hyperlinks are replaced.
The reason is most likely, that there is text directly next to the tag.
Hell<a href="http://www.example.com">o old chap!
Specifically the "o" part and the "hell" part is blocking the java .replaceAll function, from doing it's job.
Expected Result
Hello [1] old chap!
EDIT:
If I would just add space, before and after the URL, it might split some words like "help", into "hel p", which is also not an option.
My code would have to replace the URL tag (without the ) and create no new extra spaces.
This is some of my code, where the problem occures:
for (int i = 0; i < EN.length; i++) {
Pattern pattern_URL = Pattern.compile("<a(.+?)\">", Pattern.DOTALL);
Matcher matcher_URL = pattern_URL.matcher(EN[i]); //Checks in the curren Array part.
if (matcher_URL.find() == true) {
source_number++;
String extractedURL = matcher_URL.group(0);
//System.out.println(extractedURL);
String extractedURL_fully = extractedURL.replaceAll("href=\"", ""); //Anführungszeichen
//System.out.println(extractedURL_fully);
String nobracketURL = extractedURL.replaceAll("\\)", ""); //Remove round brackets from URL
EN[i] = EN[i].replaceAll("\\)\"", "\""); /*Replace round brackets from URL in Array. (For some reasons there have been href URLs, with an bracket at the end. This was already in the PDF. They were causing massive problems, because it didn't comment them out, so the entire replaceAll command didn't function.)*/
EN[i] = EN[i].replaceAll(nobracketURL, "[" + source_number + "]"); //Replace URL tags with number and Edgy brackets
}
else{
//System.out.println("FALSE: " + "[" + i + "]");
}
}
The whole idea of this is, that it loops through the array and replaces all the URLs, including it's starting tag <a until the end of the starting tag "> (which can also be seen in the pattern regex.)
Correct me if I'm wrong, but what you need is to eliminate all the <a> tags from a given string, right? If that's the case all you needed to do was use a code like the following:
final String string = "<a href=\"http://www.example.com\">Sen";
final Pattern pattern = Pattern.compile("<a(.+?)>", Pattern.DOTALL);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll("");
System.out.println(result); // prints "Sen"
Notice I didn't use the replaceAll from the String object, but from the Matcher object. This replaces all matches for the empty string "".
I'm coding a Java app to store paths in a particular format, so I need to escape some characters in order to put the paths in a database, but I cannot do it properly:
The original string looks like this:
ML Database Prototype\\NAS-500\\
and I need it in this particular format:
"\"ML\ Database\ Prototype\\NAS-500\""
So far I'm trying to do it using
String str = "ML Database Prototype\\NAS-500\\";
newStr = ( "\"\""+str+"\"" ).replace(" ","\" ");
System.out.println(newStr);
""WT" Database" Prototype\\DR0151-populated"
You can use as follows and will work:
newStr = ( "\"\\\""+str+"\\\"\"" ).replace(" ","\\ ");
The output for this is:
"\"ML\ Database\ Prototype\\NAS-500\""
I've a number of subscription from a YouTube channel that I copied.
It's "4 372 236".
I'm testing the "\s+" regex on https://regex101.com for that number and it does not work. When i'm writing the same number on my own the regex does work. Anybody knows what's wrong?.
I'm trying to remove the white space chars from such numbers but i cannot do it. I tried also the .replaceAll(" ", "") method but does not work neither.
screen from regex101.com
The JSON Youtube code:
JSON Youtube
Then I'm using JSON library to get the subscriptions like this:
JSONObject jsonObject = new JSONObject();
jsonObject = new JSONObject(content);
JSONArray tabs = jsonObject.getJSONObject("contents")
.getJSONObject("twoColumnBrowseResultsRenderer")
.getJSONArray("tabs");
JSONObject tabRenderer = tabs.getJSONObject(5).getJSONObject("tabRenderer");
JSONObject sectionListRenderer = tabRenderer.getJSONObject("content").getJSONObject("sectionListRenderer");
JSONArray contents2 = sectionListRenderer.getJSONArray("contents");
JSONObject itemSectionRenderer = contents2.getJSONObject(0).getJSONObject("itemSectionRenderer").getJSONArray("contents").getJSONObject(0);
JSONObject channelAboutFullMetadataRenderer = itemSectionRenderer.getJSONObject("channelAboutFullMetadataRenderer");
String subs = channelAboutFullMetadataRenderer.getJSONObject("subscriberCountText").getJSONArray("runs").getJSONObject(0).getString("text");
And finally, i'm using the regex to delete the whitespaces from number:
subs = subs.replaceAll("\\s+", "");
System.out.println(subs);
I tried this too but it does not work. I think it's not a regular space but I don't know how to recognise it.
subs = subs.replaceAll(" ", "");
Okay guys, I found it out.
It was not a duplication of Why does String.replace not work?. I kept in my mind that string in Java are immutable.
Between the numbers there are not simple spaces. It's NO-BREAK SPACE' (U+00A0).
So, the regex should look like this
subs = subs.replaceAll("[\\u202F\\u00A0]", "");
Maybe it will help somebody in the future :) Thanks #metters
I suggest you copy that number into notepad++ and use the "show all symbols" option.
Maybe there are not only whitespaces inbetween.
EDIT: sorry for not using the comment function, i need reputation for that and it sucks.
You need to escape the backslash:
System.out.println("4 372 236".replaceAll("\\s+", ""));
prints: 4372236
Your question doesn't really explain what you are trying to accomplish and you have not provided any sort of code other than a method to go off. It really depends on what your end goal is.
Generally, when you are trying to do can be accomplished easily through the replaceAll method as mentioned.
String test = "4 372 236";
String reg = "\\s+";
String newLine = test.replaceAll(reg, "");
or simply
String test = "4 372 236";
String newLine = test.replaceAll(" ", "");
May I know the fastest way to replace two strings in java?
Here is the sample code:
String template = "I replace {0} and {1} at one time".
String replaceZero = "Java";
String replaceOne = "C#";
template = template.replace("{0}", replaceZero).replace("{1}", replaceOne);
I have to write code like this. But I want the fastest way. Like this:
String template = "I replace {0} and {1} at one time".
tempate = template.replace("{0}{1}", replaceZero, replaceOne);
Is there any implementations to replace all strings in one time?
It looks like you may be looking for MessageFormat.format. I can't guarantee that it will work fastest, but your code should be clearer.
String template = "I replace {0} and {1} at one time";
String replaceZero = "Java";
String replaceOne = "C#";
System.out.println(MessageFormat.format(template, replaceZero, replaceOne));
Output: I replace Java and C# at one time
You could use a StringBuilder. Something like,
String template = "I replace {0} and {1} at one time";
String replaceZero = "Java";
String replaceOne = "C#";
StringBuilder sb = new StringBuilder(template);
sb.replace(sb.indexOf("{0}"), sb.indexOf("{0}") + 3, replaceZero).
replace(sb.indexOf("{1}"), sb.indexOf("{1}") + 3, replaceOne);
System.out.println(sb.toString());
I get
I replace Java and C# at one time
Are you tied to your input having "{0}" and "{1}"? Is this some kind of requirement outside your control?
If you have control of the input, then I would look at using something like:
String.format("I replace %s and %s at one time", "Java", "C#");
I have a bunch of strings like this:
Some text, bla-bla http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter
And I need to parse this String to two:
Some text, bla-bla
http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter
I need separate them, but, of course, it's enough to parse only URL.
Can you help me, how can I parse url from string like this.
By using split :
String str = "Some text, bla-bla http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter";
String [] ar = str.split("http\\.*");
System.out.println(ar[0]);
System.out.println("http"+ar[1]);
This depends on how robust you want your parser to be. If you can reasonably expect every url to start with http://, then you can use
string.indexOf("http://");
This returns the index of the first character of the string you pass in (and -1 if the string does not appear).
Full code to return a substring with just the URL:
string.substring(string.indexOf("http://"));
Here's the documentation for Java's String class. Let this become your friend in programming! http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Try something like this:
String string = "sometext http://www.something.com";
String url = string.substring(string.indexOf("http"), string.length());
System.out.println(url);
or use split.
I know in PHP you'd be able to run the explode() (http://www.php.net/manual/en/function.explode.php) function. You'd choose which character you want to explode at. For instance, you could explode at "http://"
So running the code via PHP would look like:
$string = "Some text, bla-bla http://www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter";
$pieces = explode("http://", $string);
echo $pieces[0]; // Would print "Some text, bla-bla"
echo $pieces[1]; // Would print "www.easypolls.net/poll.html?p=51e5a300e4b084575d8568bb#.UeWjBcCzaaA.twitter"