I would like to trim a beginning and ending double quote (“) from a string. How can I achieve that in Java? Thanks!
Tag: string
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: public class Main{ static void …
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
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 …
How do I read / convert an InputStream into a String in Java?
If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for …
Why does Java’s hashCode() in String use 31 as a multiplier?
Per the Java documentation, the hash code for a String object is computed as: using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. Why is 31 used as a multiplier? I understand that the multiplier should be a relatively large prime number. So why not 29,
What is meant by immutable?
This could be the dumbest question ever asked but I think it is quite confusing for a Java newbie. Can somebody clarify what is meant by immutable? Why is a String immutable? What are the advantages/disadvantages of the immutable objects? Why should a mutable object such as StringBuilder be preferred over String and vice-verse? A nice example (in Java) will
How do I count the number of occurrences of a char in a String?
I have the string I want to count the occurrences of ‘.’ in an idiomatic way, preferably a one-liner. (Previously I had expressed this constraint as “without a loop”, in case you’re wondering why everyone’s trying to answer without using a loop). Answer My ‘idiomatic one-liner’ for this is: Why write it yourself when it’s already in commons lang? Spring
How to split a string with any whitespace chars as delimiters
What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (‘ ‘, ‘t’, ‘n’, etc.) as delimiters?