Java Utililty Methods String Chop at Word

List of utility methods to do String Chop at Word

Description

The list of methods to do String Chop at Word are organized into topic(s).

Method

StringchopAtWord(String string, int length)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before.
if (string == null) {
    return string;
char[] charArray = string.toCharArray();
int sLength = string.length();
if (length < sLength) {
    sLength = length;
for (int i = 0; i < (sLength - 1); i++) {
    if ((charArray[i] == '\r') && (charArray[i + 1] == '\n')) {
        return string.substring(0, i);
    else if (charArray[i] == '\n') {
        return string.substring(0, i);
if (charArray[sLength - 1] == '\n') {
    return string.substring(0, sLength - 1);
if (string.length() < length) {
    return string;
for (int i = length - 1; i > 0; i--) {
    if (charArray[i] == ' ') {
        return string.substring(0, i).trim();
return string.substring(0, length);
StringchopAtWord(String string, int length)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before.
if (string == null) {
    return string;
char[] charArray = string.toCharArray();
int sLength = string.length();
if (length < sLength) {
    sLength = length;
for (int i = 0; i < sLength - 1; i++) {
    if (charArray[i] == '\r' && charArray[i + 1] == '\n') {
        return string.substring(0, i + 1);
    else if (charArray[i] == '\n') {
        return string.substring(0, i);
if (charArray[sLength - 1] == '\n') {
    return string.substring(0, sLength - 1);
if (string.length() < length) {
    return string;
for (int i = length - 1; i > 0; i--) {
    if (charArray[i] == ' ') {
        return string.substring(0, i).trim();
return string.substring(0, length);
StringchopAtWord(String string, int length, int minLength)
Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before.
if (length < 2) {
    throw new IllegalArgumentException("Length specified (" + length + ") must be > 2");
} else if (minLength >= length) {
    throw new IllegalArgumentException("minLength must be smaller than length");
int sLength = (string == null) ? -1 : string.length();
if (sLength < 1) {
    return string;
...
StringchopAtWord(String string, int length, int minLength)
chop At Word
if (length < 2)
    throw new IllegalArgumentException((new StringBuilder()).append("Length specified (").append(length)
            .append(") must be > 2").toString());
if (minLength >= length)
    throw new IllegalArgumentException("minLength must be smaller than length");
int sLength = string != null ? string.length() : -1;
if (sLength < 1)
    return string;
...
StringchopAtWordsAround(String input, String wordList[], int numChars)
chop At Words Around
if (input == null || "".equals(input.trim()) || wordList == null || wordList.length == 0 || numChars == 0)
    return "";
String lc = input.toLowerCase();
String arr$[] = wordList;
int len$ = arr$.length;
for (int i$ = 0; i$ < len$; i$++) {
    String aWordList = arr$[i$];
    int pos = lc.indexOf(aWordList);
...
StringchopAtWordsAround(String input, String[] wordList, int numChars)
Returns a substring of the given string which represents the words around the given word.
if (input == null || "".equals(input.trim()) || wordList == null || wordList.length == 0 || numChars == 0) {
    return "";
String lc = input.toLowerCase();
for (int i = 0; i < wordList.length; i++) {
    int pos = lc.indexOf(wordList[i]);
    if (pos > -1) {
        int beginIdx = pos - numChars;
...