Author: Reprinted
Programming: compile a function to intercept a string. The input is a string and the number of characters, and the output is a byte string. However, make sure that no half of Chinese characters are cut, for example, "my ABC" 4, it should be cut to "my AB", input "my ABC Han Def", 6, the output should be "I abc" instead of "I ABC + Han half ".
Package string;
Class splitstring
{
Private string STR;
Private int bytenum;
Public splitstring (){}
Public splitstring (string STR, int bytenum)
{
This. Str = STR;
This. bytenum = bytenum;
}
Public void splitit ()
{
Byte BT [] = Str. getbytes ();
System. Out. println ("length of this string =>" + bt. Length );
If (bytenum> 1)
{
If (BT [bytenum] <0)
{
String substrx = new string (BT, 0, -- bytenum );
System. Out. println (substrx );
} Else
{
String substrex = new string (BT, 0, bytenum );
System. Out. println (substrex );
}
} Else
{
If (bytenum = 1)
{
If (BT [bytenum] <0)
{
String substr1 = new string (BT, 0, ++ bytenum );
System. Out. println (substr1 );
} Else
{
String substr2 = new string (BT, 0, bytenum );
System. Out. println (substr2 );
}
} Else
{
System. Out. println ("input error !!! Enter an integer greater than zero :");
}
}
}
}
Public class testsplitstring
{
Public static void main (string ARGs [])
{
String STR = "my ABC handef ";
Int num = 6;
Splitstring sptstr = new splitstring (STR, num );
Sptstr. splitit ();
}