Return to Snippet

Revision: 50482
at August 24, 2011 10:28 by m3rol666


Initial Code
public static String rightTrim(String data)
	{
		if(data==null)
			return null;
		
		char[]arr=data.toCharArray();
		StringBuffer s = new StringBuffer();
		char[] newArr=new char[1];
		int pos=0;
		//worst algorithm ever. but works!
		for (int i = 0; i < arr.length; i++)
		{
			pos=(arr.length-1)-i;
//			System.out.println("pos["+pos+"] i["+i+"] ["+arr[pos]+"]");
			if(arr[pos]!=' ')
			{
				newArr= new char[pos+1];
				System.arraycopy(arr, 0, newArr, 0, pos+1);
				break;
			}
				
		}
		
		return new String(newArr);
	}

Initial URL

                                

Initial Description
I made this because org.apache.commons.lang.StringUtils does not have this method. I used this because i was dealing with a legacy db where all fields were char. trailing spaces were a pita.

Initial Title
Right trim in Java

Initial Tags
java

Initial Language
Java