ツール[1]文字列string

序文

文字列は、Javaプログラミングで最も一般的に使用されるデータ型の1つ、またはjava.langパッケージで最も一般的に使用される要素の1つです。文字列は、データベースに格納される基本データ型として、または大きなテキストとして使用できます構造はフロントエンドに表示され、他のデータ型(int、long、Double、BigDecimalなど)に簡単に変換できます。また、日付をさまざまな形式に変換できます。

1.一般的な方法

インターセプト文字列メソッド

str元の文字列

valは文字列の添え字位置からインターセプトされます

    /**
	 * @author 
	 * @DateTime 2017年12月5日 下午7:56:25
	 *
	 * @param str
	 * @param val
	 * @return
	 */
	public static String subStr(String str, int val) {

		String returnValue = str;
		if (StringUtils.isNotEmpty(str) && str.length() > val) {

			returnValue = str.substring(0, val);

		} else {
			returnValue = str;
		}

		return returnValue;
	}

インターセプト文字列メソッド

str大きな文字列

spcはインターセプトされる文字列を含みます

	public static List<String> splitString(String str, String spc) {

		if (str == null || str.isEmpty() || spc == null || spc.isEmpty()) {
			return null;
		}

		List<String> strList = new ArrayList<String>();
		String[] sts = str.split(spc);

		for (String string : sts) {
			strList.add(string.toString());
		}

		return strList;
	}

空文字列判定方法

	public static boolean isEmpty(String str) {

		if (str == null || str.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

親文字列にサブ文字(文字列)が出現する回数を問い合わせます

    /** 查证字符串出现的次数
	 *  @author 
	 *	@DateTime 2018年6月12日 上午10:53:22
	 *
	 *  @param srcText
	 *  @param findText
	 *  @return
	 */
	public static int appearNumber(String srcText, String findText) {
		int count = 0;
		int index = 0;
		while ((index = srcText.indexOf(findText, index)) != -1) {
			index = index + findText.length();
			count++;
		}
		return count;
	}

インターセプト文字列メソッド

開始位置から終了位置までの切片

str:文字列

beginIndex:開始インデックス位置

endIndex:終了インデックス位置

注:閉める前に開くという原則と閉める

	/**
	 *  按起始位置截取字符串,不包含最后一位
	 *  @author 
	 *	@DateTime 2019年2月21日 下午6:02:57
	 *
	 *  @param str
	 *  @param beginIndex
	 *  @param endIndex
	 *  @return
	 */
	public static String subStrByLocation(String str, int beginIndex, int endIndex) {

		try {
			return str.substring(beginIndex, endIndex);
		} catch (Exception e) {
			log.info("",e);
			return "";
		}
	}

intへの文字列

	public static int StringToInt(String str) {

		try {
			if (str.isEmpty()) {
				return 0;
			} else {
				return Integer.valueOf(str);
			}
		} catch (Exception e) {
			log.info("StringToInt error---->" + e);
			return 0;
		}

	}

 

おすすめ

転載: blog.csdn.net/Follow_24/article/details/108760876