Java String Instance

Source: Internet
Author: User

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

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.