import org.jetbrains.annotations.Nullable; class StringSubstring { void testSubString(String s) { if (s.substring(1, 3).equals("_")) {} if (s.length() > 1) {} if (s.substring(1).isEmpty()) {} } @Nullable static String parseDir(String packageName, String dirName) { int index = packageName.length(); while (index > 0) { int index1 = packageName.lastIndexOf('.', index - 1); String token = packageName.substring(index1 + 1, index); final boolean equalsToToken = dirName.equals(token); if (!equalsToToken) { String packagePrefix = packageName.substring(0, index); if (packagePrefix.length() > 0) { return null; } return packagePrefix; } index = index1; } return null; } public static void parse(String text) { int xCnt = 0, yCnt = 0; int pos = text.length() - 1; for (; pos >= 0; --pos) { char ch = text.charAt(pos); if (ch == 'X' || ch == 'x') ++xCnt; else if (ch == 'Y' || ch == 'y') ++yCnt; else if (Character.isDigit(ch)) { ++pos; break; } } text = text.substring(0, pos); if (text.length() == 0) { throw new IllegalArgumentException(); } System.out.println(xCnt + ":" + yCnt); } public void processPrefix(String text) { String currentPrefix = text.isEmpty() ? "^" : text.substring(0, 1); if (!currentPrefix.isEmpty() && Character.isDigit(currentPrefix.charAt(0))) { currentPrefix = ""; } System.out.println(currentPrefix); } void parseText(String text) { if (text.length() < 5) { System.out.println("Short"); } else { String kind = text.substring(0, 2); String reference = text.substring(3); if (reference.length() > 1) {} } } String getShortName(String fullName) { int end = fullName.lastIndexOf(".ext"); if (end > 0) { String shortName = fullName.substring(0, end); fullName = shortName.isEmpty() ? fullName : shortName; } return fullName; } void testEmpty(String input) { String s = input.substring(0, 1); String s1 = s.substring(1); if (s1.equals("")) {} String s2 = s1.substring(0); if (s2.equals("")) {} String s3 = s2.substring(0); } void testDiff(String s1, String s2, int pos) { if (s1.substring(pos, pos+4).equals("xyz")) {} if (s1.substring(pos, pos+4).equals(s2)) { if (s2.length() != 4) {} } } void testSubstringVarRef(String s, int from, int length) { String s1 = s.substring(0, length); if (s1.length() == length) {} String s2 = s.substring(from, from + length); if (s2.length() == length) {} } public static boolean testSubstringEquals(String s1, String s2, int idx) { return s1.substring(0, idx).equals(s2.substring(0, idx)); } }