Java algorithm parsing 3 ————— to string correlation algorithm

Source: Internet
Author: User

One: Shorten the string according to a certain rule

/* * Shorten string */public class Dome07 {public static void main (string[] args) {string str = "According to the rules C:\\windows\\resources\\themes \\aero ";" while (true) {System.out.println (str); int endIndex = Str.lastindexof ("\ \");//intercept string str = str.substring (0, EndIndex); The range of the//substring (start,end) method: [Start,end] if (str.indexof ("\ \") = =-1) {System.out.println (str); return;// Exit Program}}}}


Two: Count the number of different types of characters in a string

/* * Statistics string number of different character types */public class Dome08 {public static void main (string[] args) {string str = "weather, tianqi/,//t232aa{{"; System.out.printf ("String length:%d\n", str.length ());//English, Chinese, number, other stringbuilder[] sb = new Stringbuilder[4];for (int i = 0; I &l T Sb.length; i++) {//StringBuilder initialization sb[i] = new StringBuilder ();} for (int i = 0; i < str.length (); i++) {//contains all English letters if (Str.charat (i) >= ' A ' && str.charat (i) <= ' z ') {sb[ 0].append (Str.charat (i));//Judging all Chinese} else if (Str.charat (i) >= 19968 && Str.charat (i) <= 171941) {Sb[1].appe nd (Str.charat (i));//number} else if (Str.charat (i) >= ' 1 ' && str.charat (i) <= ' 9 ') {sb[2].append (Str.charat (i) );} else {sb[3].append (Str.charat (i));}} for (int i = 0; i < sb.length; i++) {System.out.printf ("%d\t%s\n", Sb[i].length (), sb[i].tostring ());}}}


Three: Implement a function that replaces each space in a string with "%20"

For example, enter "I am Tom", then output "I%20am%20tom"

public class Dome06 {public void Replaceblank (char string[], int length) {if (string = null | | length <= 0) return;int Originallength = 0;int Numberofblank = 0;int i = 0; System.out.println (String), while (string[i] = ' ++numberofblank;++i ') {++originallength;if (string[i] = = "); int newlength = originallength + Numberofblank * 2;IF (newlength > Length) return;int indexoforiginal = Originallength;i NT Indexofnew = newlength;while (indexoforiginal >= 0 && indexofnew > Indexoforiginal) {if (string[indexofor Iginal] = = ') {string[indexofnew--] = ' 0 '; string[indexofnew--] = ' 2 '; string[indexofnew--] = '% ';} else {String[indexofne w--] = string[indexoforiginal];} --indexoforiginal;} System.out.println (string);} public static void Main (string[] args) {char[] originalstr = new Char[30];originalstr[0] = ' I '; originalstr[1] = "; origin ALSTR[2] = ' a '; originalstr[3] = ' m '; originalstr[4] = '; originalstr[5] = ' T '; originalstr[6] = ' o '; originalstr[7] = ' m '; or IGINALSTR[8] = '. '; New doMe06 (). Replaceblank (Originalstr, 30);}} 


Java algorithm parsing 3 ————— to string correlation algorithm

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.