Java tutorial
//package com.java2s; /* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2012 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { public static byte[] unpadRight(byte[] tgt, byte padding) { int len = tgt.length; for (; len > 0; len--) { if (tgt[len - 1] != padding) { break; } } if (len > 0) { byte[] rslt = new byte[len]; System.arraycopy(tgt, 0, rslt, 0, len); return rslt; } return new byte[0]; } /** * Unpad from right. * @param s - original string * @param c - padding char * @return unPadded string. */ public static String unPadRight(String s, char c) { int end = s.length(); if (end == 0) return s; while ((0 < end) && (s.charAt(end - 1) == c)) end--; return (0 < end) ? s.substring(0, end) : s.substring(0, 1); } }