Common operations for strings in Java learning Java

Source: Internet
Author: User

    1. The number of characters contained in the string, that is, the length of the string

      int length ();

    2. Get a character from a position based on location

      char charAt (int index);

    3. Gets the character's position in the string according to the character

      int indexOf (int ch) returns the position of the first occurrence of CH in the string.
      int indexOf (int ch,int fromIndex) starts at the FromIndex specified position and gets the position where Ch appears in the string.
      The int indexOf (string str) returns the position of the first occurrence of STR in the string.
      The int indexOf (string str,int fromIndex) obtains the position of STR in the string, starting at the FromIndex specified position.

    4. Whether a string contains a substring

      Boolean contains (str)

    5. Whether the string has content

      Boolean IsEmpty ()

    6. Whether the string starts with the specified content

      Boolean StartsWith (str)

    7. Whether the string ends with the specified content

      Boolean EndsWith (str)

Example:

Public class stringdemo {    public static void main (String[ ] args)  {        //String s1 =  " Abidefghijkmln ";         //method_1 (S1);         //method_trans ();         //method_replace ();         method_split ();    }     Public static void method_split ()  {        string  s =  "Zhansan,lisi,wangwu";         string[] names  = s.split (",");        for  (String name:names)  {            SYSTEM.OUT.PRINTLN (name);          }    }    public static void method_replace   ()  {        String s =  "Wfjiwfsfhssfgha";         string new_s = s.replace (' F ',  ' F ');          sop (s);         sop (new _s);    }    public static void method_trans  ()  {        char[] arr = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '};        string s = new string (arr,1,3);         sop (s);    }    public  Static void method_1 (string s)  {        sop ( S.length ());        sop (S.charat (5));//Occurs when a corner label does not exist in the accessed string stringindexoutofboundsexception         sop (S.indexof (' I ', 6));//If not found, return to -1             }    public static void sop  ( object obj)  {        system.out.println (obj);     }}


This article is from the "Azhome" blog, make sure to keep this source http://azhome.blog.51cto.com/9306775/1546370

Common operations for strings in Java learning Java

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.