This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Raw Strings in Java?
In C# there is such a thing as # ("at sign") that can be put before string if forbidden symbols occurs. For example:
#"a\b\c"
In java I have to put backslashes
"a\\b\\c"
Is there any way in Java to make this easier?
Another way may be use equvivalent code for the symbols you want to escape.
Not really. I have made the transition not long ago and at first was constantly looking for "what is C#'s equivalent in Java for xyz?"
This is sometimes helpful but mostly frustrating. C# is a much more advanced language than Java and it will take a long time for Java to catch up.
You get used to it over time :-)
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am reasonably experienced with programming, but not specifically in Java. I am coming across the following error when working in eclipse. My code is the following:
I have used the debug function, and it reports that carbonPrefix is pent, but that carbon stays at 0 throughout. Like I said, I am a novice to Java and Eclipse, so I may not be using the debug function to it's full extent.
For anybody that's interested, this the start of code where you input the name of an alkane and it tells you the formula. It worked in Javascript and I'm just trying to translate it into Java.
Thank you all so much!
you have to use
carbonPrefix.equals("pent");
in java == operator used to compare two object references and the method equals() is used to compare two strings to determine whether they are equal or not.
This question already has answers here:
Java : parse java source code, extract methods
(2 answers)
Closed 7 years ago.
I am working on a small program to compare two Java files. My goal is to compare the two files so that I can see what functions were added and deleted from one file to another (like a simple version control program). I am running into issues on how I should be handling these files. My current approach is to use a Scanner and use:
while(scanner.hasNext()) {
String function = scanner.next("((public|private|protected|static|final|native|synchronized|abstract|threadsafe|transient)+\\s)+[\\$_\\w\\<\\>\\[\\]]*\\s+[\\$_\\w]+\\([^\\)]*\\)?\\s*\\{?[^\\}]*\\}?");
System.out.println(function);
}
However this is not getting me any results for a file that I know has functions in it. Any tips or ideas on how to approach this?
You could use ANTLR Java grammar https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4 to get a full-blown Java parser and then use it to extract any information you need about Java files.
This question already has answers here:
Parse any date in Java
(6 answers)
Closed 7 years ago.
I would like to know if there is a easy way to extract the first encountered date from a String in Java.
My program will analyse a lot of String texts, in different languages. These Strings can contain a date. Because of the languages (and the different sources), I have an awful lot of formats to take into consideration.
I first thought about Regex, making one regex for each format I could find... But there are an awful lot, for exemple "Month (d)d, yyyy" or "mm/dd/yyyy" or "dd-mon-yyyy"...
So I wanted to know if there is an easier way to extract date from a String maybe by using DateFormat, so I can convert the found date to "dd/mm/yyyy".
Thank you for your help. ^^
I think the best solution is to use a regex, but obviously you have to know all the possible patterns.
A (possible) way to do this is by means of machine learning: you can provide a set of representative examples and let the algorithm finds the patterns for you.
Your problem is really similar to the one addressed in this article.
You can try to use this webapp to find a good regular expression for you.
The main problem is that you have to provide significant examples.
I hope this will help you!
This question already has answers here:
Does Java have support for multiline strings?
(43 answers)
Closed 7 years ago.
For JAVA development I need writing to a files with strings like "\r\t\n <>", because from Java I want writing a PHP file. If you can't understand look at this example:
BufferedWriter buffW = new BufferedWriter(fileW);
buffW.write("<?php\n\n\tclass MyClass {\n\tpublic function test()\n}\n}\n?>");
This is mess a code, I want to write a clean such as PHP can do, but can't find on Java alternative way as example:
<?php
$mystring = <<<EOT
This is some PHP text.
It is completely free
I can use "double quotes"
and 'single quotes',
plus $variables too, which will
be properly converted to their values,
you can even type EOT, as long as it
is not alone on a line, like this:
EOT;
?>
Is there a heredoc equivalent in Java?
No, there is no direct heredoc alternative in the Java programming language.
Check this similar question.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java “?” Operator for checking null - What is it? (Not Ternary!)
Java Null-pointer-safe accessor
Recently I read in one of the java forums about ?. operator. They wrote that ?. could not make it to the java 7. Can anybody explain what exactly ?. is?
Also, I like to know if this operator has any specific name or not like ?: is known as ternary operator.
It's called the Null-safe operator.
I believe you mean the Null-safe operator, explained here. It was under consideration for Java 7, but subsequently dropped.
The other common use for the ? in Java is the ternary operator, which has been in Java since the dark ages and is explained here.
The two are completely different features however, the only common element is that they use the ? in some way.