Java
strings
A string is a sequence of characters. Strings are a fundamental part of most programs, so Java has several built-in features that make string manipulation easy. Java has a built-in class in the java.lang package that encapsulates the data structures of a string. This class, called String, is an object representation of a character array that cannot be changed. There is an accompanying class called StringBuffer, which is used to create strings that can be modified after they are created.
The java.lang package contains two string classes: String and StringBuffer. The String class is used when working with strings that cannot be changed. On the other hand, StringBuffer is used when you want to manipulate the content of a string. The Java development environment provides two classes to manipulate and store character-type data: String, for constant strings, and StringBuffer, for strings that can change.
Since they are constant, Strings are more efficient (use less memory) than StringBuffers and can be shared. Therefore, it is important to use String whenever appropriate.
Many strings are created from string literals. When the compiler finds a series of characters in quotes (" and "), it creates a String object whose value is the text itself. The general scheme is as follows:
1String name = "Hello world";
You can also create String objects just like any other Java object: using new.
1String s = new String("Hello World.");
The above constructor is equivalent, but the first method is much more efficient since the second method creates two String objects instead of just one.
Literal strings can be used anywhere a String object can be used. For example, System.out.println() accepts a String argument, so you can use a literal string in its place:
1System.out.println("Hello World!");
+ operator. The following code snippet concatenates three strings to produce its output:1"The input has " + counter + " characters."
Two of the concatenated strings are literal strings: "The input has " and " characters.". The third string - the one in the middle - is actually an integer that is first converted to a string and then concatenated with the others.
String is length(), which returns the number of characters in a string:1String s = "abc"; 2System.out.println(s.length());
The result of running the above code would print 3, which corresponds to the length of s.
An interesting point in Java is that an object instance is created for each literal String, so you can call methods directly on a quoted string, as if it were an object reference. This example would again print 3:
1System.out.println("abc".length());
1"abc".charAt(1)
This will return 'b'.
If you need to extract more than one character at a time, you can use the getChars method, which allows you to specify the index of the first character and the last character plus one that you want to copy, as well as the char array where you want to place those characters.
1String s = "This is not a song"; 2char buf[] = new char[2]; 3s.getChars(5, 7, buf, 0);
equals method of the String class. It will return true if the only parameter consists of the same characters as the object calling equals. An alternative form of equals called equalsIgnoreCase ignores whether the characters being compared are uppercase or lowercase.1String string1="pepe"; 2String string2="juan"; 3if (string1.equals(string2)) 4{ 5 //code 6} 7if (string1.equalsIgnoreCase(string2)) 8{ 9 // code 10}
The equals method and the == operator perform two completely different equality tests. While the equals method compares the characters contained in a String, the == operator compares two object references to see if they refer to the same instance. Therefore, you should not use the == sign because it would be a binary comparison of memory pointers and would not return the correct value.
If you want to compare strings for sorting, one option is to use the compareTo() method of the String class. This method returns 0 if both strings have the same content, a negative value if the String is less than the parameter passed, and a positive value if it is greater.
1if (string1.compareTo(string2) == 0) 2System.out.println("string1 and string2 are equal"); 3else if (string1.compareTo(string2) < 0) 4System.out.println ("string1 comes before string2"); 5else if (string1.compareTo(string2) > 0) 6System.out.println("string2 comes after string1");
1String_object.toLowerCase(); // Converts to lowercase. 2String_object.toUpperCase(); // Converts to uppercase.
Sometimes it is convenient or necessary to convert an object to a String because it needs to be passed to a method that only accepts Strings. For example, System.out.println() does not accept StringBuffers, so you need to convert the StringBuffer to a String to print it.
1class ReverseString { 2 public static String reverseIt(String source) { 3 int i, len = source.length(); 4 StringBuffer dest = new StringBuffer(len); 5 for (i = (len - 1); i >= 0; i--) { 6 dest.append(source.charAt(i)); 7 } 8 return dest.toString(); 9 } 10}
The String class provides a static method valueOf(). You can use this method to convert variables of different types to a String.
1System.out.println(String.valueOf(Math.PI));
To extract a portion of a string, you can use the substring method.
1String str="The Java language"; 2String subStr=str.substring(12);
This will obtain the substring "Java".
1String subStr=str.substring(3, 11);
This will obtain the substring "language".