Java tutorial
/* * * * Copyright (C) 2014 * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * */ package org.knight.examples.guava.strings; import com.google.common.base.CaseFormat; import com.google.common.base.CharMatcher; import com.google.common.base.Charsets; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; import static org.knight.examples.guava.Utils.log; /** * 6.1 strings examples. */ public class StringsExamples { public void run() { log("-----Joiner------------------------------"); join(); log("-----Splitter------------------------------"); split(); log("-----CharMatcher------------------------------"); charMatcher(); log("-----Charsets------------------------------"); charsets(); log("-----CaseFormat------------------------------"); caseFormat(); } private void join() { //null strings are ignored String s1 = Joiner.on(',').skipNulls().join("Hello", "World", "", null, "Java", "Guava", null); log("s1: " + s1); //use "null" strings instead of null strings String s2 = Joiner.on(";;").useForNull("null").join("This", null, "will", "replaced", "", "with", "null"); log("s1: " + s2); List<String> list = Lists.newArrayList(); for (int i = 0; i < 5; i++) { list.add("sequence" + i); } StringBuilder sb = new StringBuilder(); sb.append("init value."); //append the values in the list to the StringBuilder Joiner.on(' ').skipNulls().appendTo(sb, list); log("Joiner StringBuilder: " + sb.toString()); //join Map entries. The order of the iteration is unspecified Map<String, String> map = Maps.newHashMap(); for (int i = 0; i < 5; i++) { map.put("key" + i, "value" + i); } String s3 = Joiner.on(';').withKeyValueSeparator(":").join(map); log("Map Joiner: " + s3); } private void split() { //result is ["foo ", " bar ", "", ""] String s3 = "foo , bar ,,"; Iterable<String> it1 = Splitter.on(',').split(s3); StringBuilder sb1 = new StringBuilder(); sb1.append("s3 split size=" + Iterables.size(it1)); sb1.append("||"); for (String s : it1) { sb1.append(s); sb1.append("||"); } log(sb1.toString()); //result is ["hello", "java,guava", "world"] String s4 = "hello; java,guava ; world;"; //if no omitEmptyStrings(), the result is ["hello", "java,guava", "world", ""] List<String> list = Splitter.on(';').trimResults().omitEmptyStrings().splitToList(s4); log("split list size=" + list.size() + ": " + list.toString()); String s5 = "guava,learning,best;foo"; //split by fixed length Iterable<String> it2 = Splitter.fixedLength(5).split(s5); StringBuilder sb2 = new StringBuilder(); sb2.append("s5 split size=" + Iterables.size(it2)); sb2.append("||"); for (String s : it2) { sb2.append(s); sb2.append("||"); } log(it2.toString()); Iterable<String> it3 = Splitter.on(',').limit(2).omitEmptyStrings().split(s5); StringBuilder sb3 = new StringBuilder(); sb3.append("s5 limit split size=" + Iterables.size(it3)); sb3.append("||"); for (String s : it3) { sb3.append(s); sb3.append("||"); } log("s5 limit size=" + Iterables.size(it3) + ": " + it3.toString()); } private void charMatcher() { String u = "UPPER CASE LETTER"; String l = "lower case letter"; String uld = "LOWER case And Upper Case LETTER and 8912 digits 0000"; //return false log("CharMatcher.JAVA_LOWER_CASE.matches('A'): " + CharMatcher.JAVA_LOWER_CASE.matches('A')); //retain only the upper case letters(in this case, spaces will be removed). String s1 = CharMatcher.JAVA_UPPER_CASE.retainFrom(u); log("s1: " + s1); //s2 is "lowr lttr"(2 spaces) String s2 = CharMatcher.anyOf("case").removeFrom(l); log(s2); //replace all digits with "-" //result is "LOWER case And Upper Case LETTER and ---- digits ----" String s3 = CharMatcher.DIGIT.replaceFrom(uld, '-'); log(s3); //replace all consecutive digits with a single "-" //result is "LOWER case And Upper Case LETTER and - digits -" String s4 = CharMatcher.DIGIT.collapseFrom(uld, '-'); log(s4); //contains digit, so return false boolean b = CharMatcher.inRange('a', 'z').matchesAllOf(uld); log("matchesAllOf(a-z): " + b); } private void charsets() { String s = "Charsets class does not throw UnsupportedEncodingException"; //if use JDK7, use StandardCharsets instead byte[] b = s.getBytes(Charsets.UTF_8); log("b.length=" + b.length); } private void caseFormat() { String s1 = "CamelNamingRule"; //return camel-naming-rule log("convert to LOWER_HYPHEN: " + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s1)); String s2 = "HYPHEN-Naming-Rule"; //use Converter interface to convert hyphen style to underscore style //the source format does not conform to the assumed format //so the behavior of this method is undefined //this Converter.convert() actually calls CaseFormat.to() method in the last step log("convert to LOWER_UNDERSCORE: " + CaseFormat.LOWER_HYPHEN.converterTo(CaseFormat.LOWER_UNDERSCORE).convert(s2)); } }