Skip to content

Instantly share code, notes, and snippets.

@xzzz9097
xzzz9097 / stringInString.java
Last active August 29, 2015 14:06
Java - Insert a string into another string
/**
* Function that inserts a specified string into another string. It uses java's StringBuilder.
* Usage: insertStringInString("dogs cats", "and", 4) -> "dogs and cats"
* @param string - The main string that should contain the new piece
* @param insertion - The piece to add
* @param position - The position from which to add the piece
* @return - The final string
*/
public static String insertStringInString(String string, String insertion, int position) {
// Only insert if the position is > -1 (useful with string.indexOf which returns -1
@xzzz9097
xzzz9097 / removePartOfString.java
Last active August 29, 2015 14:05
Remove part of string - Java
/**
* Quick method to remove a specified part of a string
* @param remove - the string part to remove
* @param from - the original string
* @return - the cut string
*/
public static String removeStringFromString(String remove, String from) {
char lastCharacter = remove.charAt(remove.length() - 1);
int cutStart = from.lastIndexOf(lastCharacter) + 1;
return from.substring(cutStart);