Appreciate, respect and accept it, don't take it for granted

Java String 常用操作

2016-08-10

轉換(transform)-魔形女

Some Data Type -> String

Integer -> String

1
2
3
4
5
6
7
8
9
10
int number = 1234;

// public static String valueOf(int i)
String.valueOf(number)

// public static String toString(int i)
Integer.toString(number)

// Let JVM do it
"" + number

char -> String

1
2
char c = '0';
String.valueOf(char);

int Array -> String

1
2
3
int[] array = {1,2,3,4};
String strArr = Arrays.toString(array);
// strArr is "[1, 2, 3, 4]"

byte Array -> String

1
2
// bytes is byte[] type
String testString = new String(bytes, StandardCharsets.UTF_8);

String -> Some Data Type

Some Data Type: int, long, BigInteger, BigDecimal, double, byte[], char[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
String number = "1234";
String bigDecimal = "1234.5678";
String doubleN = "3.14";

// String to int
int intNum = Integer.parseInt(number);

// String to long
long longNum = Long.parseLong(number);

// String to BigInteger
BigInteger bigIntegerNum = new BigInteger(number);

// String to BigDecimal
BigDecimal bigDecimalNum = new BigDecimal(bigDecimal);

// String to double
double doubleNum = Double.parseDouble(doubleN);

// Handle the "NumberFormatException"
1
2
// String to byte[]
byte[] bytes = "1234".getBytes(StandardCharsets.UTF_8);
1
2
3
4
5
6
// String to char[]
String s = "1234";
char[] charArray = s.toCharArray();

// if use 3rd party library (Apache commons-lang)
Character[] charObjectArray = ArrayUtils.toObject(charArray);
1
2
3
4
5
6
7
// Arrays.asList
String pokemon = "Bulbasaur,Charmander,Squirtle,Pikachu";
String[] pokemons = pokemon.split(",");
List<String> pokemonList = Arrays.asList(pokemons);

// new ArrayList
List<String> pokemonList2 = new ArrayList<String>(Arrays.asList(pokemons));

比較(compare)

equals, equalsIgnoreCase, compareTo, compareToIgnoreCase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
String str1 = "abc";
String str2 = new String("abc");
String str3 = new String("aBc");

// True
str1.equals(str2)
// False
str1.equals(str3)
// False
str2.equals(str3)

// 0
str1.compareTo(str2)
// 32
str2.compareTo(str3)
// -32
str3.compareTo(str2)

子集合(subset)

substring, 使用開始(beginIndex)與結束標記(endIndex)取得不包含結束標記的子字串。

1
2
3
4
5
6
7
String str1 = new String("0123456789");

// 56789
str1.substring(5)

// 5678
str1.substring(5, 9)

分割(split)-次元刀

splitStringTokenizer,注意escape character

split

1
2
3
4
5
6
7
// public String[] split(String regex)
String pokemon = "Bulbasaur,Charmander,Squirtle,Pikachu";
String[] pokemons = pokemon.split(",");

String pokemonCP = "Bulbasaur235Charmander167Squirtle3310Pikachu9527";
// {"Bulbasaur","Charmander","Squirtle","Pikachu"}
String[] pokemons = pokemonCP.split("\\d+");

StringTokenizer

1
2
3
4
5
6
// Default set of characters are empty spaces (\t\n\r\f)
String pokemon = "Bulbasaur Charmander Squirtle Pikachu";
StringTokenizer tokenizer = new StringTokenizer(pokemon);
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}

合併(join)

+,String的concat, StringBuffer或StringBuilder的append

延伸議題:StringBuilder vs String concatenation in toString() in Java

1
2
3
4
5
6
7
8
9
String str1 = "Pikachu is";
String str2 = " cute";

str1 = str1.concat(str2);

StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
sb.toString();

Blog comments powered by Disqus