How do I truncate a java string to fit in a given number of bytes, once UTF-8 encoded?

Here is a simple loop that counts how big the UTF-8 representation is going to be, and truncates when it is exceeded: public static String truncateWhenUTF8(String s, int maxBytes) { int b = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // ranges from http://en.wikipedia.org/wiki/UTF-8 int skip = … Read more

How to truncate a string in groovy?

The Groovy community has added a take() method which can be used for easy and safe string truncation. Examples: “abscd adfa dasfds ghisgirs fsdfgf”.take(10) //”abscd adfa” “It’s groovy, man”.take(4) //”It’s” “It’s groovy, man”.take(10000) //”It’s groovy, man” (no exception thrown) There’s also a corresponding drop() method: “It’s groovy, man”.drop(15) //”n” “It’s groovy, man”.drop(5).take(6) //”groovy” Both take() … Read more

Truncating Query String & Returning Clean URL C# ASP.net

System.Uri is your friend here. This has many helpful utilities on it, but the one you want is GetLeftPart: string url = “http://www.website.com/default.aspx?utm_source=twitter&utm_medium=social-media”; Uri uri = new Uri(url); Console.WriteLine(uri.GetLeftPart(UriPartial.Path)); This gives the output: http://www.website.com/default.aspx [The Uri class does require the protocol, http://, to be specified] GetLeftPart basicallys says “get the left part of the uri … Read more

Text-overflow CSS truncation

Works here: http://jsfiddle.net/TReRs/4/ .overme { width: 300px; overflow:hidden; white-space:nowrap; text-overflow: ellipsis; color: red; } <div class=”overme”> how much wood can a woodchuck chuck if a woodchuck could chuck wood? </div> Trailing dots/ellipsis are colored in red using that basic CSS.

Truncate table(s) with rails console

The accepted answer only works if you need to recreate the whole database. To drop a single table (with the callbacks) and to get the IDs to start from 1: Model.destroy_all # Only necessary if you want to trigger callbacks. ActiveRecord::Base.connection.execute(“TRUNCATE #{table_name} RESTART IDENTITY”) If you are using Sqlite, it does not support truncate so … Read more

Limiting double to 3 decimal places

Doubles don’t have decimal places – they’re not based on decimal digits to start with. You could get “the closest double to the current value when truncated to three decimal digits”, but it still wouldn’t be exactly the same. You’d be better off using decimal. Having said that, if it’s only the way that rounding … Read more

How to truncate a file in C?

If you want to preserve the previous contents of the file up to some length (a length bigger than zero, which other answers provide), then POSIX provides the truncate() and ftruncate() functions for the job. #include <unistd.h> int ftruncate(int fildes, off_t length); int truncate(const char *path, off_t length); The name indicates the primary purpose – … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)