📄 stringhelper.java
字号:
/* * Static String formatting and query routines. * Copyright (C) 2001,2002 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utilities * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * See COPYING.TXT for details. */package com.Ostermiller.util;import java.util.HashMap;/** * Utilities for String formatting, manipulation, and queries. * More information about this class is available from <a target="_top" href= * "http://ostermiller.org/utils/StringHelper.html">ostermiller.org</a>. */public class StringHelper { /** * Pad the beginning of the given String with spaces until * the String is of the given length. * <p> * If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @return padded String. * @throws NullPointerException if s is null. */ public static String prepad(String s, int length){ return prepad(s, length, ' '); } /** * Pre-pend the given character to the String until * the result is the desired length. * <p> * If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @param c padding character. * @return padded String. * @throws NullPointerException if s is null. */ public static String prepad(String s, int length, char c){ int needed = length - s.length(); if (needed <= 0){ return s; } StringBuffer sb = new StringBuffer(length); for (int i=0; i<needed; i++){ sb.append(c); } sb.append(s); return (sb.toString()); } /** * Pad the end of the given String with spaces until * the String is of the given length. * <p> * If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @return padded String. * @throws NullPointerException if s is null. */ public static String postpad(String s, int length){ return postpad(s, length, ' '); } /** * Append the given character to the String until * the result is the desired length. * <p> * If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @param c padding character. * @return padded String. * @throws NullPointerException if s is null. */ public static String postpad(String s, int length, char c){ int needed = length - s.length(); if (needed <= 0){ return s; } StringBuffer sb = new StringBuffer(length); sb.append(s); for (int i=0; i<needed; i++){ sb.append(c); } return (sb.toString()); } /** * Pad the beginning and end of the given String with spaces until * the String is of the given length. The result is that the original * String is centered in the middle of the new string. * <p> * If the number of characters to pad is even, then the padding * will be split evenly between the beginning and end, otherwise, * the extra character will be added to the end. * <p> * If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @return padded String. * @throws NullPointerException if s is null. */ public static String midpad(String s, int length){ return midpad(s, length, ' '); } /** * Pad the beginning and end of the given String with the given character * until the result is the desired length. The result is that the original * String is centered in the middle of the new string. * <p> * If the number of characters to pad is even, then the padding * will be split evenly between the beginning and end, otherwise, * the extra character will be added to the end. * <p> * If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @param c padding character. * @return padded String. * @throws NullPointerException if s is null. */ public static String midpad(String s, int length, char c){ int needed = length - s.length(); if (needed <= 0){ return s; } int beginning = needed / 2; int end = beginning + needed % 2; StringBuffer sb = new StringBuffer(length); for (int i=0; i<beginning; i++){ sb.append(c); } sb.append(s); for (int i=0; i<end; i++){ sb.append(c); } return (sb.toString()); } /** * Split the given String into tokens. * <P> * This method is meant to be similar to the split * function in other programming languages but it does * not use regular expressions. Rather the String is * split on a single String literal. * <P> * Unlike java.util.StringTokenizer which accepts * multiple character tokens as delimiters, the delimiter * here is a single String literal. * <P> * Each null token is returned as an empty String. * Delimiters are never returned as tokens. * <P> * If there is no delimiter because it is either empty or * null, the only element in the result is the original String. * <P> * StringHelper.split("1-2-3", "-");<br> * result: {"1", "2", "3"}<br> * StringHelper.split("-1--2-", "-");<br> * result: {"", "1", ,"", "2", ""}<br> * StringHelper.split("123", "");<br> * result: {"123"}<br> * StringHelper.split("1-2---3----4", "--");<br> * result: {"1-2", "-3", "", "4"}<br> * * @param s String to be split. * @param delimiter String literal on which to split. * @return an array of tokens. * @throws NullPointerException if s is null. */ public static String[] split(String s, String delimiter){ int delimiterLength; // the next statement has the side effect of throwing a null pointer // exception if s is null. int stringLength = s.length(); if (delimiter == null || (delimiterLength = delimiter.length()) == 0){ // it is not inherently clear what to do if there is no delimiter // On one hand it would make sense to return each character because // the null String can be found between each pair of characters in // a String. However, it can be found many times there and we don' // want to be returning multiple null tokens. // returning the whole String will be defined as the correct behavior // in this instance. return new String[] {s}; } // a two pass solution is used because a one pass solution would // require the possible resizing and copying of memory structures // In the worst case it would have to be resized n times with each // resize having a O(n) copy leading to an O(n^2) algorithm. int count; int start; int end; // Scan s and count the tokens. count = 0; start = 0; while((end = s.indexOf(delimiter, start)) != -1) { count++; start = end + delimiterLength; } count++; // allocate an array to return the tokens, // we now know how big it should be String[] result = new String[count]; // Scan s again, but this time pick out the tokens count = 0; start = 0; while((end = s.indexOf(delimiter, start)) != -1) { result[count] = (s.substring(start, end)); count++; start = end + delimiterLength; } end = stringLength; result[count] = s.substring(start, end); return (result); } /** * Replace occurrences of a substring. * * StringHelper.replace("1-2-3", "-", "|");<br> * result: "1|2|3"<br> * StringHelper.replace("-1--2-", "-", "|");<br> * result: "|1||2|"<br> * StringHelper.replace("123", "", "|");<br> * result: "123"<br> * StringHelper.replace("1-2---3----4", "--", "|");<br> * result: "1-2|-3||4"<br> * StringHelper.replace("1-2---3----4", "--", "---");<br> * result: "1-2----3------4"<br> * * @param s String to be modified. * @param find String to find. * @param replace String to replace. * @return a string with all the occurrences of the string to find replaced. * @throws NullPointerException if s is null. */ public static String replace(String s, String find, String replace){ int findLength; // the next statement has the side effect of throwing a null pointer // exception if s is null. int stringLength = s.length(); if (find == null || (findLength = find.length()) == 0){ // If there is nothing to find, we won't try and find it. return s; } if (replace == null){ // a null string and an empty string are the same // for replacement purposes. replace = ""; } int replaceLength = replace.length(); // We need to figure out how long our resulting string will be. // This is required because without it, the possible resizing // and copying of memory structures could lead to an unacceptable runtime. // In the worst case it would have to be resized n times with each // resize having a O(n) copy leading to an O(n^2) algorithm. int length; if (findLength == replaceLength){ // special case in which we don't need to count the replacements // because the count falls out of the length formula. length = stringLength; } else { int count; int start; int end; // Scan s and count the number of times we find our target. count = 0; start = 0; while((end = s.indexOf(find, start)) != -1) { count++; start = end + findLength;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -