Why does this code using random strings print “hello world”? The following print statement would print "hello world". Could anyone explain this? System.out.println(randomString(-229985452) + " " + randomString(-147909649)); And randomString() looks like this: public static String randomString(int i) { Random ran = new Random(i); StringBuilder sb = new StringBuilder(); while (true) { int k = ran.nextInt(27); if (k == 0) break; sb.append((char)('`' + k)); } return sb.toString(); } answer: When an instance of java.util.Random is constructed with a specific seed parameter (in this case -229985452 or -147909649), it follows the
undefined
How to convert a String to an int in Java? How can I convert a String to an int in Java? My String contains only numbers and I want to return the number it represents. For example, given the string "1234" the result should be the number 1234. Solution: int foo = Integer.parseInt("1234"); See the Java Documentation for more information. (If you have it in a StringBuilder (or the ancient StringBuffer), you'll need to do Integer.parseInt(myBuilderOrBuffer.toString()); instead). http://stackoverflow.com/questions/5585779/how-to-convert-a-string-to-an-int-in-java
undefined
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 Stringto handle passwords. Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]. Solution: Strings are immutable. That means once you've created the String, if another process can dump memory, there's no way (aside from reflection) you can get rid of the data before garbage collection kicks in. With an array,
undefined
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 example, so I can write the contents of the stream to a log file). What is the easiest way to take the InputStream and convert it to a String? public String convertStreamToString(InputStream is) { // ??? } Answer: A nice way to do this is using Apache commons IOUtils to copy the InputStream into a StringWriter... something like StringWriter
undefined
Check range of Unicode Value of a character In Objective-c... If I have a character like "∆" how can I get the unicode value and then determine if it is in a certain range of values. For example if I want to know if a certain character is in the unicode range of U+1F300 to U+1F6FF Answer: NSString uses UTF-16 to store codepoints internally, so those in the range you're looking for (U+1F300 to U+1F6FF) will be stored as a surrogate pair (four bytes). Despite its name, characterAtIndex: (and unichar) doesn't know about codepoints
undefined
How do I make the first letter of a string uppercase in JavaScript? How do I make the first letter of a string uppercase, but not change the case of any of the other letters? For example: this is a test -> This is a test the Eiffel Tower -> The Eiffel Tower /index.html -> /index.html Most Best Solution: Another solution: function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } You could also add it to the String.prototype so you could chain it with other methods:
undefined
What is the difference between single-quoted and double-quoted strings in PHP? I'm not an expert in PHP programming, but I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes. I just know in .NET, or C language, if it is in single quote, that means it is a character, not a string. Solution: PHP strings can be specified not just in two ways, but in four ways. Single quoted strings will display things almost completely "as is."
undefined
Why does ++[[]][+[]]+[+[]] return the string “10”? This is valid and returns the string "10" in JavaScript (more examples here): ++[[]][+[]]+[+[]] Why? What is happening here? Solution: If we split it up, the mess is equal to: ++[[]][+[]] + [+[]] In JavaScript, it is true that +[] === 0. + converts something into a number, and in this case it will come down to +"" or 0 (see specification details below). Therefore, we can simplify it (++ has precendence over +): ++[[]][0] + [0] Because [[]][0] means: get the first element from [[]], it is true that: [[]][0] returns the inner