In a Java interview, write a function to intercept a string!!!!
Source: Internet
Author: User
function | String programming: Write a function that intercepts the string, input as a string and byte number, and output as a byte-truncated string. But to ensure that the Chinese character is not truncated half, such as "I abc" 4, should be truncated to "I ab", input "my ABC-def", 6, should be exported as "my 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!!! Please enter an integer greater than 0: ");
}
}
}
}
public class Testsplitstring
{
public static void Main (String args[])
{
String str= "I ABC-han def";
int num=6;
splitstring sptstr = new splitstring (str,num);
Sptstr.splitit ();
}
}
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