Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the number of characters in a string, and chopping off the rest

I need to create a summary table at the end of a log with some values that are obtained inside a class. The table needs to be printed in fixed-width format. I have the code to do this already, but I need to limit Strings, doubles and ints to a fixed-width size that is hard-coded in the code.

So, suppose I want to print a fixed-width table with

    int,string,double,string     int,string,double,string     int,string,double,string     int,string,double,string      and the fixed widths are: 4, 5, 6, 6. 

If a value exceeds this width, the last characters need to be cut off. So for example:

    124891, difference, 22.348, montreal 

the strings that need to be printed ought to be:

    1248 diffe 22.348 montre 

I am thinking I need to do something in the constructor that forces a string not to exceed a certain number of characters. I will probably cast the doubles and ints to a string, so I can enforce the maximum width requirements.

I don't know which method does this or if a string can be instantiated to behave taht way. Using the formatter only helps with the fixed-with formatting for printing the string, but it does not actually chop characters that exceed the maximum length.

like image 888
Flethuseo Avatar asked Dec 03 '11 18:12

Flethuseo


People also ask

How do I limit characters in a string in Java?

The indexing is done within the maximum range. It means that we cannot store the 2147483648th character. Therefore, the maximum length of String in Java is 0 to 2147483647. So, we can have a String with the length of 2,147,483,647 characters, theoretically.

How do you trim the length of a string?

Java String trim()The Java String class trim() method eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in Java string checks this Unicode value before and after the string, if it exists then the method removes the spaces and returns the omitted string.

How do I limit string input?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.


2 Answers

You can also use String.format("%3.3s", "abcdefgh"). The first digit is the minimum length (the string will be left padded if it's shorter), the second digit is the maxiumum length and the string will be truncated if it's longer. So

System.out.printf("'%3.3s' '%3.3s'", "abcdefgh", "a"); 

will produce

'abc' '  a' 

(you can remove quotes, obviously).

like image 61
Jaksa Avatar answered Oct 21 '22 02:10

Jaksa


Use this to cut off the non needed characters:

String.substring(0, maxLength);  

Example:

String aString ="123456789"; String cutString = aString.substring(0, 4); // Output is: "1234"  

To ensure you are not getting an IndexOutOfBoundsException when the input string is less than the expected length do the following instead:

int maxLength = (inputString.length() < MAX_CHAR)?inputString.length():MAX_CHAR; inputString = inputString.substring(0, maxLength); 

If you want your integers and doubles to have a certain length then I suggest you use NumberFormat to format your numbers instead of cutting off their string representation.

like image 29
GETah Avatar answered Oct 21 '22 03:10

GETah