[ Published on -
1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
a) Simple User entity class
package basics.basicsExamples; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Locale; import java.util.regex.Pattern; /** **************************************************************************** * STRING EXAMPLE CLASS * @author stanislav.zorjan */ public class StringExample { private String str0 = "šta ćeš ti... ским с чим, This is string for manipulation {1}, {2}, 123123434"; private String str1 = new String("This is... also string"); private String str2 = Byte.toString(new Byte("45")); private String str3 = Short.toString(new Short("12")); private String str4 = Integer.toString(new Integer(122)); private String str5 = Long.toString(new Long(123L)); private String str6 = Float.toString(new Float(123.0)); private String str7 = Double.toString(new Double(22.22)); private String str8 = Boolean.toString(true); private String str9 = Character.toString('c'); private String str10 = Arrays.toString(new Integer[]{1,2,3,4}); private String str11 = Arrays.toString(new int[]{1,2,3,4}); /** ************************************************************************ * Default constructor */ public StringExample() { //printStrings(); //stringOperations(); buildingStrings(); } /** ************************************************************************ * Printing strings */ private void printStrings(){ System.out.println(str0); System.out.println(str1); System.out.println(str2); System.out.println(str3); System.out.println(str4); System.out.println(str5); System.out.println(str6); System.out.println(str7); System.out.println(str8); System.out.println(str9); System.out.println(str10); System.out.println(str11); } /** ************************************************************************ * String operations */ private void stringOperations(){ // returns character at specified position/index System.out.println(str0.charAt(31)); // decimal number representation of character at specified position/index // http://www.tamasoft.co.jp/en/general-info/unicode-decimal.html System.out.println(str0.charAt(0)+" : "+str0.codePointAt(0)); System.out.println(str0.charAt(21)+" : "+str0.codePointAt(21)); System.out.println(str0.charAt(31)+" : "+str0.codePointAt(31)); // decimal representation of character, 1 before specified position/index System.out.println(str0.charAt(31)+" : "+str0.codePointBefore(32)); System.out.println(str0.codePointCount(0, 32)); // comparing string with another string System.out.println(str0.compareTo(str1)); // comparing string ignoring case System.out.println(str0.compareToIgnoreCase(str1)); // attaches "str1" at the end of the "str0" creating new string System.out.println(str0.concat(str1)); System.out.println(str0); // returns true/false if string contains specified string System.out.println(str0.contains("This is")); // comparing character sequence in a string, but doesn't compare // if "str1" is of the same type as "str0" System.out.println(str0.contentEquals(str1)); System.out.println(str0.contentEquals(new StringBuffer(str0))); // checks if string ends with specified sufix System.out.println(str0.endsWith("123123434")); // comparing if "str1" is of the same type as "str0" // and also if character sequence of "str1" is same as in "str0" System.out.println(str0.equals(str1)); // same as equals with ignoring case System.out.println(str0.equalsIgnoreCase(str1)); // returning string bytes System.out.println(Arrays.toString(str1.getBytes())); System.out.println(Arrays.toString(str1.getBytes(Charset.forName("ISO-8859-1")))); // strings hash code System.out.println(str0.hashCode()); // index on which specified string is found. If specified string is not // found, than it will return -1; System.out.println(str0.indexOf("This is")); System.out.println(str0.indexOf("This iss")); // index of decimal representation of character System.out.println(str0.indexOf(1095)); // index of decimal representation of character // searched from specified position System.out.println(str0.indexOf(1095, 22)); // internalized string System.out.println(str0.intern()); // returns true if string length is 0 System.out.println(str0.isEmpty()); // the last index/occurrence of the specified string. // Everything else is the same as in the case of // "string.indexOf" method System.out.println(str0.lastIndexOf("a")); // returns length of the string System.out.println(str0.length()); // returns true/false if "str0" matches to "str1" //or regular expression pattern System.out.println(str1.matches(str1)); System.out.println(str1.matches(Pattern.compile(str1).pattern())); // don't understand it System.out.println(str1.offsetByCodePoints(8, 8)); // tests if 2 "substrings" matches. // @param toOffset // @param string2 // @param fromIndex // @param length System.out.println(str0.regionMatches(5, str1, 5, 1)); // the same as method before with additional parameter // that specifies if char case should be ignored System.out.println(str0.regionMatches(true, 5, str1, 5, 1)); // replaces specified string with new string System.out.println(str0.replace("This is", "This is not")); // replaces all occurrences with new one System.out.println(str0.replaceAll("a", "xxx")); // replaces only first occurrence with new one System.out.println(str0.replaceFirst("a", "xxx")); // splits string into array System.out.println(Arrays.toString(str1.split(""))); System.out.println(Arrays.toString(str1.split("\\."))); System.out.println(Arrays.toString(str1.split("\\.", 2))); // returns true/false if string starts with specified string System.out.println(str1.startsWith("This is")); // returns true/false if string starts with specified string // from specified offset System.out.println(str1.startsWith("This is", 2)); // substrings string returning CharSequence System.out.println(str1.subSequence(10, 20)); // substrings string System.out.println(str1.substring(10)); System.out.println(str1.substring(10, 20)); // converts string to character array System.out.println(Arrays.toString(str1.toCharArray())); // converts string to lowercase System.out.println(str1.toLowerCase()); // converts string to lowercase System.out.println(str1.toLowerCase(Locale.FRENCH)); // converts string to uppercase System.out.println(str1.toUpperCase()); // converts string to uppercase System.out.println(str0.toUpperCase(Locale.FRENCH)); } /** ************************************************************************ * Building strings example method */ private void buildingStrings() { // Building strings using StringBuilder and StringBuffer // More info on StringBuilder and StringBuffer at: http://kaioa.com/node/59 // // StringBuilder is not synhronized so it is // a little bit faster than StringBuffer // StringBuilder strb = new StringBuilder(); StringBuilder strb1 = new StringBuilder(100); //alocated capacity StringBuilder strb2 = new StringBuilder("A little bit longer String Builder Example"); strb.append("String Builder Example"); System.out.println(strb); strb.appendCodePoint(370); System.out.println(strb); // Returns allocated space for placing characters in. // If String/char sequence is larger than allocated space, than // StringBuilder will alocate more space. System.out.println(strb.capacity()); System.out.println(strb1.capacity()); System.out.println(strb2.capacity()); // deletes characters in range // fromIndex // toIndex strb.delete(2, 5); System.out.println(strb); // deletes character ad specified index position strb.deleteCharAt(4); System.out.println(strb); // reverse the character order strb.reverse(); System.out.println(strb); // Building strings using StringBuffer // All methods and properties are of StringBuffer are same // as in StringBuilder StringBuffer sb = new StringBuffer(); StringBuffer sb1 = new StringBuffer(100); StringBuffer sb2 = new StringBuffer("Example String"); } }