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

I got error “The DELETE statement conflicted with the REFERENCE constraint”

The error means that you have data in other tables that references the data you are trying to delete. You would need to either drop and recreate the constraints or delete the data that the Foreign Key references. Suppose you have the following tables dbo.Students ( StudentId StudentName StudentTypeId ) dbo.StudentTypes ( StudentTypeId StudentType ) … Read more

Truncate string on whole words in .NET C#

Try the following. It is pretty rudimentary. Just finds the first space starting at the desired length. public static string TruncateAtWord(this string value, int length) { if (value == null || value.Length < length || value.IndexOf(” “, length) == -1) return value; return value.Substring(0, value.IndexOf(” “, length)); }