Java tutorial
//package com.java2s; //License from project: Open Source License import android.support.annotation.NonNull; import java.util.ArrayList; import java.util.List; public class Main { @NonNull public static String cleanupTagList(@NonNull final Object value, final char firstCharacter) { String tags = (String) value; String[] lines = ((String) value).split("[\\p{Space}\\p{Punct}\\r?\\n]"); List<String> nonTrivialLines = new ArrayList<>(); for (String line : lines) { String s = cleanupTag(line, firstCharacter); if (s.length() > 1) { nonTrivialLines.add(s); } } StringBuffer sb = new StringBuffer(tags.length()); for (String line : nonTrivialLines) { sb.append(line); sb.append("\n"); } String cleanedUpTagList = sb.toString().trim(); if (!tags.equals(cleanedUpTagList)) { return cleanedUpTagList; } return tags; } /** * Return the original object if no cleanup is done, or for example "" if there are zero characters worth * keeping after cleanup * <p> * Cleanup means conformance to "#tagnumberlowercasedigitsnotinfirstposition" * * @param value * @return */ @NonNull public static String cleanupTag(@NonNull final Object value, final char firstCharacter) { final String tag = ((String) value); String cleanedTag = tag.trim().toLowerCase(); while (cleanedTag.startsWith("##") || cleanedTag.startsWith("@@")) { cleanedTag = cleanedTag.substring(1); } final StringBuffer sb = new StringBuffer(cleanedTag.length()); boolean first = true; for (char c : cleanedTag.toCharArray()) { if (!Character.isLetter(c)) { // Is not a letter if (!first && Character.isDigit(c)) { sb.append(c); } continue; } sb.append(c); first = false; } String s = sb.toString(); if (!s.startsWith("" + firstCharacter)) { s = firstCharacter + s; } if (!s.equals(tag)) { return s; } return tag; } }