Java - String

java

https://dzone.com/articles/the-dos-and-donts-of-java-strings

// String

// Join a list of Strings
import org.apache.commons.lang.StringUtils;
List myList = new ArrayList();
myList.add("...");
myList.add("...");
String result = StringUtils.join(myList , ",");

// StringBuffer
StringBuffer s3 = new StringBuffer(“Value1”);
String 2 = “Value2”;
for (int i = 0; i < 100000; i++) {
  s3.append(s2);
}

// StringBuffer is threadsafe so it has it own overhead. The other 
// alternative is StringBuilder.

if (a.equals("apples")) { ... }
if (a.equalsIgnoreCase(c)) { ... }

// Get string length:
String a = "apples";
int len = a.length();

String[] splitArray = input.split("\\s+");

// Embed Unicode characters in a string:
String s = "S\u00ED Se\u00F1or";

startsWith("AB");
endsWith("AB");
charAt(1)
length()
contains("AB")
toLowerCase()
toUpperCase()
substring(startIndex)
substring(startIndex, endIndex); // does not include the character at endIndex
split(character) // split the string using the specified character as delimiter
concat

String str = "1,2,3,4";
String[] splitup = str.split(",");

If we have a list or an array of strings, how can we join them together?

import org.apache.commons.lang.StringUtils;
List myList = new ArrayList();
myList.add("...");
myList.add("...");
String result = StringUtils.join(myList , ",");

Why should we be careful with string concatenation inside a loop?

We should be careful with string concatenation inside a loop because for each loop iteration, new object is allocated. The easiest way to solve the above problem is by using StringBuffer:

StringBuffer s3 = new StringBuffer(“Value1”);
String 2 = “Value2”;
for (int i = 0; i < 100000; i++) {
  s3.append(s2);
}

StringBuffer is threadsafe so it has it own overhead. The other alternative is StringBuilder.

What are the differences between String and StringBuffer?

  1. Objects of type String are immutable. StringBuffer is used to represent values that can be modified. In situations where values are modified a number of times, StringBuffer yields significant performance benefits.
  2. Both String and StringBuffer are thread-safe
  3. StringBuffer is implemented by using synchronized keyword on all methods

What are differences between StringBuilder and StringBuffer?

StringBuilder is not thread-safe, so, it performs better in situation were thread-safety is not required.

What is a string constant pool?

It is a part of the heap space. It is used to store all the string literals that are found in the source code.

How can we generate a random string?

String fName = "";
fName += Long.toString(Math.abs(new java.util.Random().nextLong()), 5);
fName += usersId + hcoid + ".xls";

String filename = basePath + "/" + java.util.UUID.randomUUID() + ".xls";
Long.toHexString(Double.doubleToLongBits(Math.random()));

I not sure what is the limitation with this approach. If we are using JDK 1.5, we can use UUID.randomUUID(). See http://mynotes.wordpress.com/2009/07/23/java-generating-random-string/

How can we declare an array of Strings?

String[] strHeaderNames = { "USERSID", "LOGINID", "FIRSTNAME", "LASTNAME", "WINDOWS CREDENTIAL"};

How can we compare strings:

String a = "apples";
String b = "bucky";
String c = "BUCKY";
if (a.equals("apples")) { ... }
if (a.equalsIgnoreCase(c)) { ... }

The == operator compares objects, not content, therefore we can not use it to compare strings. In order for two objects to be equal, they must be the same (reside at the same address)

How can we get the length of a string:

String a = "apples";
int len = a.length();

How can we split a string into an array?

String[] splitArray = input.split("\\s+");

How can we embed Unicode characters in a string?

String s = "S\u00ED Se\u00F1or";

What are the differences between == and .equals of String objects?

The == operator test the references, but does not test the content.

What are the methods available with a String object?

  1. startsWith("AB");
  2. endsWith("AB");
  3. charAt(1)
  4. length()
  5. contains("AB")
  6. toLowerCase()
  7. toUpperCase()
  8. substring(startIndex)
  9. substring(startIndex, endIndex); // does not include the character at endIndex
  10. split(character) // split the string using the specified character as delimiter
  11. concat

How can we split a string into an array?

String str = "1,2,3,4";
String[] splitup = str.split(",");

How can we represent octal character inside a string?

\ddd

How can we represent a Unicode character inside a string?

\uxxxx
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License