String
String
public class Str
{
public static void main(String args[])
{
String s = "abcdefghijk";
//substring
System.out.println(s.substring(0, 2));
//concat
String s2 = "lmnopqrst";
System.out.println(s.concat(s2));
System.out.println(s+s2);
//format
String s3;
s3 = String.format("Year: %d", 2017);
System.out.println(s3);
//float to string
String s4 = String.valueOf(3.14);
System.out.println(s4);
//string to float
float f = Float.valueOf("3.14");
System.out.println(f);
//split
s = "575-571-0350";
String [] array = s.split("-");
for(int i = 0; i < array.length; i++)
System.out.println(array[i]);
//replace
System.out.println(s.replace('-', '.'));
//equals, ==
if(s.equals("575.571.0350"))
System.out.println("Equals");
else
System.out.println("Not Equals");
if(s == "575.571.0350")
System.out.println("==");
else
System.out.println("!=");
}
}
- equals(), test equality of two objects
- ==, compare reference, whether they are the same object
StringBuilder
- String is immutable, StringBuilder is mutable
public class Str
{
public static void main(String args[])
{
//String is immutable
String s = "Hello";
System.out.println(System.identityHashCode(s));
s = "World!";
System.out.println(System.identityHashCode(s));
//s[0] = 'H'; // not able to change a character
//StringBuilder is mutable
StringBuilder sb = new StringBuilder("Hello World!");
//append
sb.append(3.14);
System.out.println(sb);
//insert
sb.insert(0, 3.14);
System.out.println(sb);
//delete
sb.delete(0, 4);
System.out.println(sb);
//replace
sb.replace(0, 1, "Hello");
System.out.println(sb);
//reverse
System.out.println(sb.reverse());
}
}
StringTokenizer
import java.util.StringTokenizer;
public class Token
{
public static void main(String args[])
{
StringTokenizer t = new StringTokenizer("1, 2, 3, 4", ", ");//multiple delimiters
System.out.printf("Number of Tokens: %d%n", t.countTokens());
while(t.hasMoreTokens())
{
System.out.println(t.nextToken());
}
}
}
Regular Expression
public class Reg
{
public static boolean validPhoneNumber(String s)
{
return s.matches("\\d{3}[=.]\\d{3}[-.]\\d{4}");
}
public static void main(String args[])
{
String phoneNumber = "575.571.0350";
System.out.println(validPhoneNumber(phoneNumber));
}
}
Pattern and Matcher
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Reg
{
public static void main(String args[])
{
Pattern p = Pattern.compile("\\d{3}[-.]\\d{3}[-.]\\d{4}");
String s = "575.571.0350, 575.571.1583";
//Pattern
System.out.println(Pattern.matches("\\d{3}[-.]\\d{3}[-.]\\d{4}.*", s));
System.out.println(Pattern.quote(s));
//Matcher
Matcher m = p.matcher(s);
while(m.find())
{
System.out.println(m.group());
}
}
}
Text Boundary
- BreakIterator, natural text language
- StreamTokenizer, programming language
import java.util.*;
import java.text.*;
public class S
{
public static void main(String args[])
{
Locale currentLocale = new Locale ("en","US");
BreakIterator itr = BreakIterator.getWordInstance(currentLocale);
itr.setText("Hello Lin, how is everything going?");
//get the start and end of each word
int boundary = itr.first();
while(boundary != BreakIterator.DONE)
{
System.out.println(boundary);
boundary = itr.next();
}
}
}
Reference