Java String Strip stripLastElement(String str)

Here you can find the source of stripLastElement(String str)

Description

strip Last Element

License

Open Source License

Declaration

public static String stripLastElement(String str) 

Method Source Code

//package com.java2s;
/**//from   w ww.  j  ava  2  s .c  o m
 * Copyright 2001-2014 CryptoHeaven Corp. All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of CryptoHeaven Corp. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with CryptoHeaven Corp.
 */

import java.util.*;

public class Main {
    public static String stripLastElement(String str) {
        String last = getLastElement(str);
        if (last.length() > 0) {
            int index = str.lastIndexOf(last);
            if (index >= 0) {
                if (index == 0)
                    str = "";
                else
                    str = str.substring(0, index);
                str = str.trim();
                while (str.endsWith(",") || str.endsWith(";")) {
                    str = str.substring(0, str.length() - 1).trim();
                }
            }
        }
        return str;
    }

    public static String getLastElement(String str) {
        String lastToken = "";
        str = str.trim();
        if (str.endsWith(",") || str.endsWith(";"))
            lastToken = "";
        else {
            StringTokenizer st = new StringTokenizer(str, ",;");
            lastToken = str;
            while (st.hasMoreTokens())
                lastToken = st.nextToken().trim();
        }
        return lastToken;
    }
}

Related

  1. strip(final String s)
  2. strip(String string, String token)
  3. stripBadHTMLTags(String s)
  4. stripClassFromClassName(String className)
  5. stripExtension(String filename)
  6. stripLeadingSlash(String str)
  7. stripNamespaceDeclarations(String xml)
  8. stripTags(final String tagged)
  9. stripTags(String html)