java - jruby trim string based on length with ellipses -


This is a very specific problem, but I am thinking that someone has a great solution. I'm trying to fit a string with a certain font size in a box of arbitrary size, for example, if the whole string is not fit, then I want to trim it and an alpi ad (...) .

So if my text is "the Netherlands", then I want to determine that whatever part of my box's arbitrary shape fits, it looks like "downwards ..."

I'm using Jruby, so Java or Ruby is OK. To fit the string I can not come around with trimming / four-four-character testing / testing in addition to a superb solution. Since this is not a fixed width, so I can not take each variable as the same width, which will make it very easy.

Any ideas or suggestions that can point me in the right direction?

Character-by-character loop is probably OK. But if you are doing billions in them and need to sharpen them, you can do binary-search of the right length instead of just walking through the characters. Here is a normal binary_access method that you can call on integer. It's a & lt; => Block quotation - which gives 0 or + x, and it jumps over and over again for as long as it returns to the test or 0 or the jump size is very low.

  square integer def binary_search current = self to nextjump = current / 2.0 (test = yield (current)) == 0 || Next Jump & lt; 0.5 If the test & lt; 0 current + = next jump and current - = next jump end nextjump / = 2.0 end current end end  

Then use this like this:

  target = Whatever is [0 ... (a.size.binary_search {| i | one [0 ... i]. Stringing - target})  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -