Say you have a String literal with a lot of quotation marks inside it. You could escape them all, but it’s a pain, and difficult to read. In some languages, you can just do this: In Java, however, ” is used for chars, so you can’t use it for Strings this way. Some languages have syntax to work around this.
Tag: string
String, StringBuffer, and StringBuilder
Please tell me a real time situation to compare String, StringBuffer, and StringBuilder? Answer Mutability Difference: String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Difference: The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. So when the application needs
How to add new elements to an array?
I have the following code: Those two appends are not compiling. How would that work correctly? Answer The size of an array can’t be modified. If you want a bigger array you have to instantiate a new one. A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a )
How can I trim beginning and ending double quotes from a string?
I would like to trim a beginning and ending double quote (“) from a string. How can I achieve that in Java? Thanks!
Why doesn’t String’s hashCode() cache 0?
I noticed in the Java 6 source code for String that hashCode only caches values other than 0. The difference in performance is exhibited by the following snippet: Running this in ideone.com gives the following output: So my questions are: Why doesn’t String’s hashCode() cache 0? What is the probability that a Java string hashes to 0? What’s the best
Java: splitting a comma-separated string but ignoring commas in quotes
I have a string vaguely like this: that I want to split by commas — but I need to ignore commas in quotes. How can I do this? Seems like a regexp approach fails; I suppose I can manually scan and enter a different mode when I see a quote, but it would be nice to use preexisting libraries. (edit:
Java: convert List to a join()d String
JavaScript has Array.join() Does Java have anything like this? I know I can cobble something up myself with StringBuilder: .. but there’s no point in doing this if something like it is already part of the JDK. Answer String.join With Java 8 you can do this without any third party library. If you want to join a Collection of Strings
Prevent round off in String.format(“%.2f”, doubleValue) in Java
How do I prevent String.format(“%.2f”, doubleValue); from rounding off (round half up algorithm) instead of just truncating it? e.g. after formatting, I just want to discard the last digit, I know there are other ways to do this, I just want to know if this is possible using the String.format. Answer You can always set the rounding mode: http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html and
How do I find out if first character of a string is a number?
In Java is there a way to find out if first character of a string is a number? One way is and do the above all the way till 9, but that seems very inefficient. Answer Note that this will allow any Unicode digit, not just 0-9. You might prefer: Or the slower regex solutions: However, with any of these
What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?
The title pretty much says it all. What’s the simplest/most elegant way that I can convert, in Java, a string from the format “THIS_IS_AN_EXAMPLE_STRING” to the format “ThisIsAnExampleString”? I figure there must be at least one way to do it using String.replaceAll() and a regex. My initial thoughts are: prepend the string with an underscore (_), convert the whole string