This function has been applied repeatedly to multiple mobile phone application software platforms
Usage: Parameter definition: str--string to split
font--fonts
rowmaxw--width per line after split
Support designator:
\ n Line Change
\ t Inserts a space of two Chinese characters length
public static final string[] Clipstring (String str,font font,int rowmaxw) {
if (str = null)
return null;
if (Rowmaxw < Font.charwidth (' ha '))
Rowmaxw = font.charwidth (' ha ');
int strid = 0;
int roww = 0;
Vector strmanager = new vector ();
Char ch = ';
while (Str.length () > Strid) {
ch = str.charat (Strid);
Switch (CH)
{
case ' \ n ':
Strmanager.addelement (str.substring (0,strid));
str = str.substring (strid+1);
Roww = 0;
Strid = 0;
break;
case ' \ t ':
StringBuffer sb = new StringBuffer (str);
Sb.deletecharat (Strid);
Sb.insert (Strid, "");
str = sb.tostring ();
break;
Default:
if (Roww + font.charwidth (ch) > Rowmaxw) {
Strmanager.addelement (str.substring (0,strid));
str = str.substring (Strid);
Roww = 0;
Strid = 0;
}else{
Roww + = font.charwidth (ch);
strid++;
}
}
}
Strmanager.addelement (str);
string[] O_str = new string[strmanager.size ()];
Strmanager.copyinto (O_STR);
return o_str;
}