Saturday, 22 December 2007

String manipulation

Guys, have you all been waiting for my next lesson? :)

Today I'll show you how to do a very trivial task in challenge solving - string manipulation. With Java everything is easy :)

- Get the length of a string:
int len = str.length();
- Access the nth character of a string:
char c = str.charAt(index);
Note that in Java indices start at 0.
- Create a String from a char array:
String s = new String(charArray);
- Convert a String to a char array:
char[] c = str.toCharArray()
- Create a String from a byte array:
String s = new String(byteArray)
- Convert a String to a byte array:
byte[] b = str.getBytes();
- Get a substring of a string:
String sub1 = str.substring(beginIndex);
String sub2 = str.substring(beginIndex, endIndex);
- Find the first occurence of a character or a substring:
int index1 = str.indexOf(c);
int index2 = str.indexOf(str2);
int index3 = str.indexOf(c, fromIndex);
If there is no occurence, the result is -1.
- Compare 2 strings: strings are objects, so you can't compare strings with ==. Instead you can use:
boolean b1 = str1.equals(str2);
int b2 = str1.compareTo(str2);
int b3 = str1.equalsIgnoreCase(str2);
- Concatenate 2 strings:
String s1 = str1 + str2;
String s2 = str1.concat(str2);
But when you are concatenating many strings repeatedly you should consider using StringBuffer or StringBuilder for better performance.
- Test if a string contains another string:
boolean b = str1.contains(str2);
- Check if str2 is at the beginning of str1:
boolean b = str1.startsWith(str2);
- Check if str2 is at the end of str1:
boolean b = str1.endsWith(str2);
- Convert a string to uppercase/lowercase:
String str2 = str1.toUpperCase();
String str3 = str1.toLowerCase();
- Replace characters in a string:
// Replace all a's with b's
String str2 = str1.replace('a', 'b');
// Replace all hello's with hola's
String str3 = str1.replace("hello", "hola");
// Replace all digits with 0s
String str4 = str1.replaceAll("[0-9]", "0");
Regular expressions are so powerful but yet so huge that they will be covered in a later topic :P
- Check if a string matches a pattern:
// Check if str is a hexadecimal representation
boolean b = str.matches("[0-9A-Fa-f]+");
- Convert from String to primitives:
boolean b1 = Boolean.valueOf(str);
int i1 = Integer.valueOf(str);
double d1 = Integer.valueOf(str);
// The parseXXX methods are more powerful
boolean b2 = Boolean.parseBoolean(str);
int i2 = Integer.parseInt(str);
double d2 = Double.parseDouble(str);
// Binary conversion
int i3 = Integer.parseInt(str, 2);
// Octal conversion
int i4 = Integer.parseInt(str, 8);
// Hex conversion
int i5 = Integer.parseInt(str, 16);
- Convert from primitives to String:
// boolean b = true;
String str1 = Boolean.toString(b);
// int i = 1337;
String str2 = Integer.toString(i);
// Binary conversion
String str3 = Integer.toString(i, 2);
String str4 = Integer.toBinaryString(i);
// Octal conversion
String str5 = Integer.toString(i, 8);
String str6 = Integer.toOctalString(i);
// Hex conversion
String str7 = Integer.toString(i, 16);
String str8 = Integer.toHexString(i);
// double d = 1.337;
String str9 = Double.toString(d);
- There's also this very powerful method:
// The result will be: "true 1337 53a 1.337"
String str1 = String.format("%b %d %x %1.3f", true, 1337, 1338, 1.337);
For a complete format string reference see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html
- Reverse a string:
String str2 = new StringBuffer(str1).reverse().toString();
Probably more will come later when I have the time :P

No comments: