public UTF8String trimRight ( UTF8String trimString ) { if ( trimString == null ) return null ; int charIdx = 0 ; // number of characters from the source string int numChars = 0 ; // array of character length for the source string int [ ] stringCharLen = new int [ numBytes ] ; // array of the first byte position for each character in the source string int [ ] stringCharPos = new int [ numBytes ] ; // build the position and length array while ( charIdx < numBytes ) { stringCharPos [ numChars ] = charIdx ; stringCharLen [ numChars ] = numBytesForFirstByte ( getByte ( charIdx ) ) ; charIdx += stringCharLen [ numChars ] ; numChars ++ ; } // index trimEnd points to the first no matching byte position from the right side of // the source string. int trimEnd = numBytes - 1 ; while ( numChars > 0 ) { UTF8String searchChar = copyUTF8String ( stringCharPos [ numChars - 1 ] , stringCharPos [ numChars - 1 ] + stringCharLen [ numChars - 1 ] - 1 ) ; if ( trimString . find ( searchChar , 0 ) >= 0 ) { trimEnd -= stringCharLen [ numChars - 1 ] ; } else { break ; } numChars -- ; } if ( trimEnd < 0 ) { // empty string return EMPTY_UTF8 ; } else { return copyUTF8String ( 0 , trimEnd ) ; } }
Based on the given trim string trim this string starting from right end This method searches each character in the source string starting from the right end removes the character if it is in the trim string stops at the first character which is not in the trim string returns the new string .