J2-based string separation based on display width

Source: Internet
Author: User

Author: a drop of blue

/*** Split a long string into a String Array Based on the specified width and font ** @ Param longstring * used to split a long string * @ Param font * used to measure the font of the string width * @ Param width * display the width of a single line string on the device * @ return split into a string array */private string [] splitlongstring1_stringarray (string longstring, font font, int width) {If (font. stringwidth (longstring) <= width) {return New String [] {longstring};} else {int stringlength = longstring. length (); char [] chars = longstring. tochararray (); vector v = new vector (); int offset = 0, length = 1; while (Offset + Length <stringlength) {If (font. charswidth (chars, offset, length) <width) {length ++;} else {v. addelement (longstring. substring (offset, offset + length); offset = offset + length; length = 1 ;}} if (offset <stringlength) {v. addelement (longstring. substring (offset);} string [] strings = new string [v. size ()]; V. copyinto (strings); Return strings ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.