Code Practice--java-4-String __ajax

Source: Internet
Author: User
Tags stringbuffer

Welcome to visit the blog Create string Create string Object-1

public class Test {public
    static void Main (string[] args) {
        string s = new string ();//create String
        System.out.print ln (s);
    }
}
Create a String Object-2
public class Test {public
    static void Main (string[] args) {
        char a[] = {' G ', ' o ', ' o ', ' d '};
        string s = new String (a); Create string
        System.out.println (a);
    }
}
Create a String Object-3
public class Test {public
    static void Main (string[] args) {
        char[] a = {' s ', ' t ', ' u ', ' d ', ' e ', ' n ', ' t '};
        string s = new String (A, 2, 4);
        System.out.println (s);
    }
string ManipulationString manipulation-stitching (+)
public class Test {public
    static void Main (string[] args) {
        string S1 = new String ("Hello");
        String s2 = new String ("World");
        String s = S1 + "" + S2;
        System.out.println (s);
    }
String manipulation-stitching (+)
public class Test {public
    static void Main (string[] args) {
        int booktime = 4;
        float practice = 2.5f;
        System.out.println ("I spend every day" + Booktime + "hour reading;" + Practice + "hours on the Machine");
    }
String manipulation-Get string length
public class Test {public
    static void Main (string[] args) {
        String s = "We are students";
        System.out.println ("Length of string is:" + s.length ());
    }
String manipulation-Gets the index position of the specified character
public class Test {public
    static void Main (string[] args) {
        String s = "We are students";
        The position of the System.out.println ("character S" in the string S is: "+ s.indexof (" s "));
        The position of the System.out.println ("character St" in the string S is: "+ s.indexof (" St "));
    }
public class Test {public
    static void Main (string[] args) {
        String s = "We are students";
        System.out.print (the last position of the character s in the string S is: ");
        System.out.println (S.lastindexof ("s"));
    }
String operation-Gets the character at the specified index position
public class Test {public
    static void Main (string[] args) {
        String s = ' Hello World ';
        Char mychar2 = S.charat (6);
        System.out.println (character bit with index position 6 in string s: + mychar2);
    }
String manipulation-remove leading and trailing spaces
public class Test {public
    static void Main (string[] args) {
        String S1 = "   Java class    ";
        String s2 = S1.trim ();
        System.out.println ("The original length of the string:" + s1.length ());
        System.out.println ("Remove space after the length:" + s2.length ());
        System.out.println ("The string to remove whitespace is:" + s2);
    }
String manipulation-Removes all spaces in a string
Import Java.util.StringTokenizer;
public class Test {public
    static void Main (string[] args) {
        String text = "  We are student  ";
        System.out.println ("Original string is:");
        System.out.println (text);
        StringTokenizer st = new StringTokenizer (text, "");
        StringBuffer sb = new StringBuffer ();
        int i = 1;
        while (St.hasmoreelements ()) {
            i++;
            Sb.append (St.nexttoken ());
        }
        System.out.println ("The string that removes all spaces in the string is:");
        System.out.println (Sb.tostring ());

    }
public class Test {public
    static void Main (string[] args) {
        String s = "J a V" a programming word  code  ;
        s = S.replaceall ("", "");
        System.out.println ("The string after removing the space is:" + s);
    }
String manipulation-string substitution
public class Test {public
    static void Main (string[] args) {
        String s = ' bad Bad study ';
        String news = S.replace ("Bad", "good");

        System.out.println ("The replaced string is:" + news);
    }
}
public class Test {public
    static void Main (string[] args) {
        String s = ' bad Bad study ';
        s = S.replacefirst ("Bad", "good");

        System.out.println ("The replaced string is:" + s);
    }
String operations-determining whether strings are equal
public class Test {public
    static void Main (string[] args) {
        string S1 = new String ("I am a Student");
        String s2 = new String ("I am a Student");
        String s3 = new String ("I AM A STUDENT");
        String s4 = S1;

        Boolean B1 = (S1 = = s2);
        Boolean B2 = (S1 = = S4);
        Boolean b3 = s1.equals (s2);
        Boolean b4 = s1.equals (S3);
        Boolean b5 = s1.equalsignorecase (s2);
        Boolean b6 = S1.equalsignorecase (S3);

        System.out.println ("S1 = = S2:" + B1);
        System.out.println ("S1 = = S4:" + B2);
        System.out.println ("S1 equals s2:" + B3);
        System.out.println ("S1 equals S3:" + B4);
        System.out.println ("S1 equalsignorecase S2:" + B5);
        System.out.println ("S1 equalsignorecase S3:" + B6);
    }
String manipulation-determines the beginning and end of a string
public class Test {public
    static void Main (string[] args) {
        String num1 = "22045612";
        String num2 = "21304578";

        Boolean B1 = Num1.startswith ("a");
        Boolean b2 = Num1.endswith ("the");
        Boolean b3 = Num2.startswith ("a");
        Boolean b4 = Num2.endswith ("the");

        System.out.println ("String Num1" starts with ' 22 '). "+ B1);
        System.out.println ("String Num1" ends with ' 78 '). "+ b2);
        System.out.println ("String num2" starts with ' 22 '). "+ B3);
        System.out.println ("String num2" ends with ' 78 '). "+ b4);
    }
}
String manipulation-Letter capitalization conversion
public class Test {public
    static void Main (string[] args) {
        string s = new String ("ABC DEF");
        String news1 = S.tolowercase ();
        String news2 = S.touppercase ();

        System.out.println ("Original string:" + s);
        System.out.println ("All converted to lowercase character string: + news1);
        " System.out.println ("Convert all to uppercase string: + News2);
    }"
String manipulation-string segmentation
public class Test {public
    static void Main (string[] args) {
        string s = new String ("Abc,def,ghi,gkl");
        string[] News = S.split (",");

        System.out.println ("Original string:" + s);
        System.out.println ("The string divided by split character is:");
        for (int i=0; i<news.length; i++) {
            System.out.println (news[i]);}}}
public class Test {public
    static void Main (string[] args) {
        string s = new String ("Abc,def,ghi,gkl");
        string[] News2 = S.split (",", 2);

        for (int j=0; j<news2.length; J + +) {
            System.out.println ("String divided once by split character is:");
            System.out.println (News2[j]);}}
Instance-strings are split according to the specified delimiter
public class Test {public
    static void Main (string[] args) {
        String s = "No words alone on the West floor, the month as hook, lonely indus deep courtyard lock Kiyoaki." "+ 
                    " cut constantly, the reason is still disorderly, is the sadness, not the general taste in the heart. ";
        System.out.println ("source string:" + s);
        System.out.println ("The length of the source string is:" + s.length ());
        string[] News = S.split (", |. ");
        for (int i=0; i<news.length; i++) {
            System.out.println (news[i]);
        System.out.println ("The length of the string array after the branch is:" + news.length);
    }
Instance-Determines whether a string is a number format
public class Test {public
    static void Main (string[] args) {
        String s = ' 12312312 ';
        if (Isnumber (s)) {
            System.out.println (s + "is number format");
        } else {
            System.out.println (s + "not number format");
        }
    Public

    Static Boolean isnumber (String str) {
        char[] c = Str.tochararray ();
        for (int i=0; i<c.length; i++) {
            if (Character.isdigit (c[i)))
                ;
            else return
                false;
        return true;
    }
EssaysEclipse
Babel Eclipse ctrl+alt+/: auto-complement Java keyword alt+/: launch Eclipse Code Auxiliary menu

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.