Skip to content

Instantly share code, notes, and snippets.

@Loiree
Last active May 26, 2017 18:03
Show Gist options
  • Save Loiree/2152d88e5b89c99661cfc3170f876854 to your computer and use it in GitHub Desktop.
Save Loiree/2152d88e5b89c99661cfc3170f876854 to your computer and use it in GitHub Desktop.
// Методы String.
String first = " Windows win ";
first.trim(); // "Windows win"
first.lastIndexOf('w'); // 9
first.lastIndexOf('W'); // 1
first.indexOf('w'); // 6
first.indexOf('w', 8); // 9
first.indexOf("win"); // 9
first.toUpperCase(); // " WINDOWS WIN "
first.toLowerCase(); // " windows win "
first.split("s"); // [" Window", "win "]
first.replace('w', '_'); // " Windo_s _in "
first.replaceFirst("w", ""); // " Windos win "
first.replaceAll("in", ""); // " Wdows w "
// Сравнение строк
String str1 = "Thunder";
String str2 = "Thunder";
boolean result = str1.equals(str2); // true
boolean result1 = str1.equalsIgnoreCase(str2); // true
int result2 = str1.compareTo(str2); // 0, что значит строки равны
int result3 = str1.compareToIgnoreCase(str2); // 0
str1 == str2; // true
// Метод для удаления символа из строки
public static String removeChar(final String str, final char ch) {
if (str == null)
return null;
return str.replaceAll(Character.toString(ch), "");
}
// Оканчивается ли строка на ".com" или начинается с "www"
String address = "www.google.com";
boolean bool, bool2;
bool = address.endsWith(".com"); // true
bool2 = address.startsWith("www"); // true
// Метод проверки, является ли строка палиндромом (одинаково читается в обоих направлениях)
private static boolean isPalindrome(final String str) {
if (str == null)
return false;
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
return strBuilder.toString().equals(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment