public class Codec1 { // Encodes a list of strings to a single string. public String encode(List<String> strs) { StringBuilder sb = new StringBuilder(); for (String item : strs) { int length = item.length(); sb.append(length).append("/").append(item); } return sb.toString(); }
// Decodes a single string to a list of strings. public List<String> decode(String s) { List<String> result = new ArrayList<>(); int i = 0; while (i < s.length()) { int pos = s.indexOf('/', i); int itemSize = Integer.parseInt(s.substring(i, pos)); int start = pos + 1; int end = pos + itemSize + 1; if (end <= s.length()) { String item = s.substring(start, end); result.add(item); } i = end; } return result; } }
public class Codec2 { public String encode(List<String> strs) { StringBuilder sb = new StringBuilder(); for (String item : strs) { sb.append(item.length()).append("/").append(item); } return sb.toString(); }
public List<String> decode(String s) { List<String> result = new ArrayList<>(); while (!s.isEmpty()) { int pos = s.indexOf("/"); int itemSize = Integer.parseInt(s.substring(0, pos)); result.add(s.substring(pos + 1, pos + itemSize + 1)); s = s.substring(pos + itemSize + 1); } return result; } }
public class Codec { public String encode(List<String> strs) { StringBuilder sb = new StringBuilder(); for (String item : strs) { sb.append(item).append('\n'); } return sb.toString(); }
public List<String> decode(String s) { List<String> result = new ArrayList<>(); BufferedReader br = new BufferedReader(new StringReader(s)); String temp = null; try { while ((temp = br.readLine()) != null) { result.add(temp); } } catch (Exception e) {
/** * Returns the index within this string of the first occurrence of the * specified substring. * * <p>The returned index is the smallest value <i>k</i> for which: * <blockquote><pre> * this.startsWith(str, <i>k</i>) * </pre></blockquote> * If no such value of <i>k</i> exists, then {@code -1} is returned. * * @param str the substring to search for. * @return the index of the first occurrence of the specified substring, * or {@code -1} if there is no such occurrence. */ public int indexOf(String str) { return indexOf(str, 0); }
/** * Returns the index within this string of the first occurrence of the * specified substring, starting at the specified index. * * <p>The returned index is the smallest value <i>k</i> for which: * <blockquote><pre> * <i>k</i> >= fromIndex {@code &&} this.startsWith(str, <i>k</i>) * </pre></blockquote> * If no such value of <i>k</i> exists, then {@code -1} is returned. * * @param str the substring to search for. * @param fromIndex the index from which to start the search. * @return the index of the first occurrence of the specified substring, * starting at the specified index, * or {@code -1} if there is no such occurrence. */ public int indexOf(String str, int fromIndex) { return indexOf(value, 0, value.length, str.value, 0, str.value.length, fromIndex); }
/** * Returns the index within this string of the last occurrence of the * specified substring. The last occurrence of the empty string "" * is considered to occur at the index value {@code this.length()}. * * <p>The returned index is the largest value <i>k</i> for which: * <blockquote><pre> * this.startsWith(str, <i>k</i>) * </pre></blockquote> * If no such value of <i>k</i> exists, then {@code -1} is returned. * * @param str the substring to search for. * @return the index of the last occurrence of the specified substring, * or {@code -1} if there is no such occurrence. */ public int lastIndexOf(String str) { return lastIndexOf(str, value.length); }
/** * Returns the index within this string of the last occurrence of the * specified substring, searching backward starting at the specified index. * * <p>The returned index is the largest value <i>k</i> for which: * <blockquote><pre> * <i>k</i> {@code <=} fromIndex {@code &&} this.startsWith(str, <i>k</i>) * </pre></blockquote> * If no such value of <i>k</i> exists, then {@code -1} is returned. * * @param str the substring to search for. * @param fromIndex the index to start the search from. * @return the index of the last occurrence of the specified substring, * searching backward from the specified index, * or {@code -1} if there is no such occurrence. */ public int lastIndexOf(String str, int fromIndex) { return lastIndexOf(value, 0, value.length, str.value, 0, str.value.length, fromIndex); }
总结:indexOf和lastIndexOf里第二个位置参数是参与搜索的。
substring
substring(int beginIndex, int endIndex)截取的是从beginIndex开始到endIndex-1的字符串,截出来的长度为endIndex-beginIndex。
/** * Returns a string that is a substring of this string. The * substring begins at the specified {@code beginIndex} and * extends to the character at index {@code endIndex - 1}. * Thus the length of the substring is {@code endIndex-beginIndex}. * <p> * Examples: * <blockquote><pre> * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "mile" * </pre></blockquote> * * @param beginIndex the beginning index, inclusive. * @param endIndex the ending index, exclusive. * @return the specified substring. * @exception IndexOutOfBoundsException if the * {@code beginIndex} is negative, or * {@code endIndex} is larger than the length of * this {@code String} object, or * {@code beginIndex} is larger than * {@code endIndex}. */ public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > value.length) { throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } return ((beginIndex == 0) && (endIndex == value.length)) ? this : new String(value, beginIndex, subLen); }