Java Machine Test Topic _ How to intercept string

Source: Internet
Author: User
Tags truncated

Face Test 1 how to intercept a string

Questions and Answers

write a function that intercepts a string, enter it as a string and number of bytes, and output a string that is truncated by bytes. But to ensure that the Chinese character is not truncated half, such as " i ABC" 4, should be cut to " i AB", Enter " i ABC han DEF" 6, should output " I ABC ", not " i abc+ half of Han . "

Analysis of test questions

This question is prone to confusion is Chinese characters and English characters, here need to consider the character and English characters occupy the number of bytes, Chinese characters accounted for two bytes, English characters accounted for a byte, understand this, it is easy to complete the subject.

Code

     Packaget0806; ImportJava.util.Scanner;  Public classTest3 {StaticString SS;//the string to intercept operation        Static intN//The number of bytes of the intercepted string
Public Static voidMain (string[] args) {System.out.println ("Please enter a string:"); Scanner Scstr=NewScanner (system.in);//get a string from the keyboardSS = Scstr.next ();//take the contents of the scanner object as a stringSystem.out.println ("Please enter the number of bytes:"); Scanner Scbyte=NewScanner (system.in);//get a string from the keyboardn = scbyte.nextint ();//take the contents of the scanner object as a numeric value interception (SetValue ()); //the method and method of applying }
This method is good, because the common is the way the string is read in, so that any input is converted to an array, the aspect of comparison Public StaticString[] SetValue () {//The purpose of this method is to convert a string into a string arrayString[] string =NewString[ss.length ()];//creates a character array of string for(inti = 0; i < string.length; i++) {String[i]= Ss.substring (I, i + 1); //takes the I character of the string SS out and puts it in a string in the character array } returnString//This character array is returned }
Public Static voidinterception (string[] String) {intCount = 0; String m= "[\u4e00-\u9fa5]";//the regular expression test of Chinese charactersThe string System.out.println ("with each" + N + "byte) is as follows:"); for(inti = 0; i < string.length; i++) { if(String[i].matches (m)) {//matches each element in a character array to a table expression and returns True if the sameCount = Count + 2;//if the current character is a kanji, the counter count is incremented by 2}Else{Count= count + 1;//if the current character is not a kanji, the counter count is incremented by 1 } if(Count < N) {//if the value of the current counter count is less than N, the current character is outputSystem.out.print (String[i]); } Else if(count = = N) {//if the value of the current counter count equals N, the current character is outputSystem.out.print (String[i]); Count= 0; System.out.println (); //internal loop results, you need to wrap, play the role of controlling the printing format}Else{Count= 0;//if the value of the current counter count is greater than N, the counter count is zeroed, and then the outer loop is executedSystem.out.println (); } } } }

Java Machine Test Topic _ How to intercept string

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.