How to add symbol " to a String? [duplicate] - java

This question already has answers here:
Quotation marks inside a string [duplicate]
(4 answers)
Closed 8 years ago.
Is it possible to add this symbol " to a String or print to console?
I'm using it for my ImageIcon! file path thanks!

Yes, you need to escape the character with \ for Java to understand it:
String quote = "\"";

To be able to include " in a Java String you need to escape it like this
String s = "this is a String containing \" which is escaped by backslash"

Another way that was not mentioned here is the use of a char object.
String quote1 = (char)'"';
String quote2 = (char)34;

Did you mean How to print "Hello World" text on console? If yes then you can do that like this:
System.out.print("\"Hello World\"");

String str="Prashant"+"*^%$##!\" ";

you need to add before it the Escape character .

Related

Grabbing partial data from URL in Selenium [duplicate]

This question already has answers here:
Parse a URI String into Name-Value Collection
(30 answers)
Closed 4 years ago.
In selenium, is there a way to grab partial dynamically changing data from a URL? Example, I ran driver.getCurrentUrl() in my code and retrieved the below URL. I only want to grab the numeric portion of it and concatenate it so I end up with 819499-1. However, next time I run it the values will be different. Is there a way to capture this information without capturing the entire URL?
http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true
I think this is a more generic question, not only related to Selenium...
Basically, You could use regular expression matching like so:
String url = "http://custmaint/clnt_estimate_overview.jsp?estimateNbr=819499&versionNbr=1&estimateMgmt=true&clntAutoAssign=true";
final Pattern p = Pattern.compile("^.*\\?estimateNbr=(\\d+)&versionNbr=(\\d+).*$");
Matcher m = p.matcher(url);
if (m.find()) {
System.out.print(m.group(1) + "-" + m.group(2));
}
Another approach (more flexible) is to use a dedicated library like httpcomponents; see How to convert map to url query string?
String url = driver.getCurrentUrl();
String estimateNbr = url.substring(url.indexOf("eNbr=")+5, url.indexOf("&v"));
String versionNbr = url.substring(url.indexOf("nNbr=")+5, url.indexOf("&e"));
System.out.println(estimateNbr + "-" + versionNbr);

How to replace String with different slashes? [duplicate]

This question already has answers here:
java split function
(6 answers)
Closed 7 years ago.
I need to rename some paths in database.
I rename folder:
String mainFolder= "D:\test\1\data"; //folder renamed from fd
Then i need to rename all files and directories inside that folder:
String file1="D:\test\1\fd\dr.jpg";
String folder1="D:\test\1\fd\fd"; // in this case last fd needs to be renamed
String folder2="src/fd/fd/"; //fake path also needs to be renamed
What is the best and fastest way to rename that strings?
My thoughts about "/":
String folder2= "src/da/da";
String[] splittedFakePath = folder2.split("/");
splittedFakePath[splittedFakePath.length - 2] = "data";
StringBuffer newFakePath = new StringBuffer();
for (String str : splittedFakePath) {
newFakePath.append(str).append("/");
}
String after rename: src/data/da/
But when im trying split by "\":
Arrays.toString(Pattern.compile(File.separator).split(folder1));
I receive:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
Look into java's String replace(...) method.
It is wonderful for string replacement, much better than attempting a regex.
Keep in mind that real directory handling has a few special cases, which don't lend themselves well to direct string manipulation. For example '//' often gets compacted to '/' in Unix like systems, and if you care about proper directory corner-cases, then use the Java Path class

Parsing a string using regex [duplicate]

This question already has answers here:
How do I get the file name from a String containing the Absolute file path?
(13 answers)
Closed 7 years ago.
I'm trying to slpit the string /home/user/test.dat
I use String[] split = file.split("(?!.*/)"); but split[1] only returns the first character instead of the whole file name. How would I edit my regex so that it returns everything past the last forward slash?
Unless there's some compelling reason to use a regular expression, I would use the simple String.lastIndexOf(int). Something like,
String file = "/a/b/c/d/e/test.dat";
int afterSlash = file.lastIndexOf('/');
if (afterSlash > -1) {
file = file.substring(afterSlash + 1);
}
System.out.println(file);
Output of above being (the requested)
test.dat
Regex
\/((\w+)\.(\w+))$
Debuggex Demo
However, since you are using Java simply load the string into the File helper which can pull out the filename:
Java
Path p = Paths.get("C:\\Hello\\AnotherFolder\\The File Name.PDF");
String file = p.getFileName().toString();

How can i split this string in Java? [duplicate]

This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Closed 9 years ago.
I have a problem with splitting a sentence in Java
input string :
"retinol,\"3,7,11,15-tetramethyl-2,4,6,10,14-hexadecapentaenoic acid\",C034534,81485-25-8,\"Carcinoma, Hepatocellular\",MESH:D006528,Cancer|Digestive system disease,,17270033,therapeutic";
and i want to split it and get splitted terms like as follows ;
retinol
3,7,11,15-tetramethyl-2,4,6,10,14-hexadecapentaenoic acid
C034534
81485-25-8
Carcinoma, Hepatocellular
MESH:D006528
Cancer|Digestive system disease
(nothing)
17270033
therapeutic
I tried few way to solve this problem such as Pattern/Matcher and split(",")[] etc..
But, i couldn't find the answer..
As discussed in the comments, since you're parsing a CSV file, you're going to want to use a library specifically written to parse CSVs. Otherwise you'll continue to run into problems where what you write is "useless when a different patten comes out" (as you said).
However, to solve the question at hand you just have to split on a comma, ignoring commas inside of quotes. So you can do this (from this answer):
String input = "retinol,\"3,7,11,15-tetramethyl-2,4,6,10,14-hexadecapentaenoic acid\",C034534,81485-25-8,\"Carcinoma, Hepatocellular\",MESH:D006528,Cancer|Digestive system disease,,17270033,therapeutic";
String[] output = input.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
for(String s : output){
System.out.println(s);
}
This will give you this output (note the quotes and empty line):
retinol
"3,7,11,15-tetramethyl-2,4,6,10,14-hexadecapentaenoic acid"
C034534
81485-25-8
"Carcinoma, Hepatocellular"
MESH:D006528
Cancer|Digestive system disease
17270033
therapeutic
You can replace the quotes and ignore the empty line as you wish. This loop will print the exact output requested in the question:
int i=1;
for(String s : output){
if(!s.isEmpty()){
System.out.println(i++ + ". " + s.replace("\"", ""));
}
}
Output:
retinol
3,7,11,15-tetramethyl-2,4,6,10,14-hexadecapentaenoic acid
C034534
81485-25-8
Carcinoma, Hepatocellular
MESH:D006528
Cancer|Digestive system disease
17270033
therapeutic
But, please, use a library like OpenCSV.

How to validate URL in java using regex? [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I want to validate url started with http/https/www/ftp and checks for /\ slashes and checks for .com,.org etc at the end of URL using regular expression. Is there any regex patttern for URL validation?
This works:
Pattern p = Pattern.compile("(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");
Matcher m = p.matcher("your url here");
I am use the following code for that
String lRegex = "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
btw a search in google and you would find the solution by yourself.

Categories