Requirement 1: The way to achieve trim yourself.
Requirement 2: Get the Upload file name "D:\\20120512\\day12\\demo1.java".
Requirement 3: reverse the characters stored in the string object. New China Good-----> Good Country New
Requirement 4: Find the number of occurrences of a substring in the entire string.
Public class demo6 {public static void main (String[] args) {String str = " Wisdom podcasts "; SYSTEM.OUT.PRINTLN (Mytrim (str));str = "D:\\20120512\\day12\\demo1.java"; GetFileName (str); str = "New China Good"; System.out.println ("Flipped string:" + reverse (str));str = "Abcjavaabcjavaphpjava"; // Javagetcount (str, "Java");} Count substring occurrences Public static void getcount (string str,string target) {Int count = 0 ; //used to record the number of occurrences int fromindex = 0; // The record starts with that index value looking for the target substring while ((Fromindex = str.indexof (Target, fromindex))!=-1) {//If the IndexOf method returns is not-1, then the target element has been found. Count++;fromindex = fromindex+target.length ();} System.out.println ("Number of occurrences:" + cOunt);} Public static string reverse (STRING&NBSP;STR) {Char[] arr = str.tochararray (); (int startindex = 0 , endindex=arr.length-1 ; startindex<endindex; startindex++,endindex--) {char temp = arr[startindex];arr[startindex] = arr[endindex]; Arr[endindex] = temp;} Constructs a string using a character array. Return new string (arr);} Requirements 2: Get the upload file name "D:\\20120512\\day12\\demo1.java". Public static void getfilename (String path) {int index = path.lastindexof ("\ \"); String filename = path.substring (index+1); System.out.println ("File name:" + filename);} Requirement 1: The way to achieve trim yourself. Public static string mytrim (STRING&NBSP;STR) {//Convert first to character array char[] arr = Str.tochararray ();//define two variables record the start and end index values Int startindex = 0 ;int endindex = arr.length -1;//determines the index value of start while (true) {if (arr[startindex]== ' ') {startindex++;} Else{break;}} Determines the index value of end : while (true) {if (arr[endindex]== ' ') {endindex--;} Else{break;}} Intercept substring return return str.substring (startindex,endindex+1);}}
Java String Instance