Tagged Questions
332
votes
24answers
259k views
How do I compare strings in Java?
I've been using the == operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.
Is == bad? When should it ...
107
votes
18answers
112k views
Java String.equals versus ==
This code separates a string into tokens and stores them in an array of strings, and then compares a variable with the first home ... why isn't it working?
public static void main (String... ...
703
votes
23answers
389k views
Read/convert an InputStream to a String
If you have 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 this to a String (for ...
34
votes
7answers
4k views
What is the purpose of the expression “new String(…)” in Java?
While looking at online code samples, I have sometimes come across an assignment of a String constant to a String object via the use of the new operator.
For example:
String s;
...
s = new ...
302
votes
9answers
252k views
Switch Statement with Strings in Java
Why can't I switch on a String in Java?
Is this functionality going to be put into a later Java version?
Can someone point me to an article, or themselves explain why I can't do this, as in, the ...
122
votes
13answers
34k views
StringBuilder vs String concatenation in toString() in Java
Given the 2 toString() implementations below, which is prefered
public String toString(){
return "{a:"+ a + ", b:" + b + ", c: " + c +"}";
}
or
public String toString(){
StringBuilder sb = ...
39
votes
10answers
36k views
Evaluating a math expression given in string form
I am trying to write a Java routine to evaluate simple math expressions from Strings. Example strings:
"5+3" or "10-40" or "10*3"
I want to avoid a lot of if-then-else statements. How can I do this?
...
25
votes
5answers
12k views
Questions about Java's String pool
Consider this code:
String first = "abc";
String second = new String ("abc");
When using the new keyword, Java will create the abc String again right?
Will this be stored on the regular heap or ...
673
votes
9answers
50k views
Why is char[] preferred over String for passwords?
In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use Strings to handle ...
43
votes
9answers
34k views
Java: splitting a comma-separated string but ignoring commas in quotes
I have a string vaguely like this:
foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy"
that I want to split by commas -- but I need to ignore commas in quotes. How can I do this? Seems like a regexp ...
59
votes
16answers
55k views
Capitalize First Char of Each Word in a String Java
Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?
Examples:
jon skeet -> Jon Skeet
miles o'Brien -> Miles O'Brien (B ...
94
votes
17answers
148k views
How can I pad a String in Java?
Is there some easy way to pad Strings in Java?
Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.
278
votes
28answers
240k views
How to generate a random alpha-numeric string
I've been looking for a simple java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over ...
101
votes
9answers
37k views
Why does Java's hashCode() in String use 31 as a multiplier?
In Java, the hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the ...
112
votes
15answers
20k views
Why can't strings be mutable in Java and .NET?
Why is it that they decided to make string immutable in Java and .NET (and some other languages)? Why didn't they make it mutable?