Strings String 2

Source: Internet
Author: User
Tags truncated

Perspective of mind Mapping

Judging the start and end of a string

The StartsWith () and EndsWith () methods of the string class are used to determine whether a string starts and ends with the specified content, and their return values are of type Boolean.

(1) StartsWith () method

This method is used to determine whether the current string object starts with the string specified by the parameter.

Str.startswith (String prefix)

Prefix: refers to the character as a prefix.

Return value: Returns a Boolean type.

// Judging string Start                 System.out.println ("whether to start with the word" string 1 "=" +str1.startswith ("String 1"));                System.out.println ("whether to start with" string "=" + (Str.indexof ("string") ==0));

(2) EndsWith () method

This method is used to determine whether the current string object ends with the string specified by the parameter.

Str.endswith (String suffix)

Suffix: Refers to a string as a suffix.

Return value: Returns a Boolean type.

// Judging the end of a string                 str= "string constant string constant";                System.out.println ("Whether to end with" constant = "+ (Str.endswith (" constant ")));                 // Lookup Method        System.out.println ("Whether to end with" constant = "+ (Str.lastindexof (" constant ") = = (Str.length ()-2)));

Methods of interception

Intercept string
Only the start position index is passed
The character that contains the starting index position from start to finish

// methods                of interception // Intercept string         // only the start position index        is passed // The character that contains the starting index position from start to finish        System.out.println ("Intercept 1=" +str.substring (1));

Pass start and End position index
Characters that do not contain an end position
Number of truncated characters = End Position index-start position index
End Position Index >= start position index
End Position index <= string length

// Pass start and end position index         //         characters that do not contain an end position // number of truncated characters = End Position index-start position index         // End Position Index >= start position index         // End Position Index <= string length        System.out.println ("Intercept 3,5=" +str.substring (3, 5));                System.out.println ("Intercept 3,5=" +str.substring (3, 10));

Remove spaces in a string

There are two kinds of whitespace in the string, one is to remove the leading and trailing spaces of the string, and the other is to remove more spaces in the string, which can be implemented in different ways.

1. Remove leading and trailing spaces from a string

The trim () method is used to remove leading and trailing spaces of a string.

Str.trim ()

STR: any string object.

Return value: The string after leading and trailing spaces are stripped.

// go space        before and after Str1= "  Zhang  three   ";                System.out.println ("go before and after spaces =" +str1.trim ());

The result shows that this method removes the leading and trailing spaces of the string, while a space in the middle is not removed.

2. Remove all spaces in the string

Removing more spaces in a string can be accomplished using the Replace method.

The method is to replace the space "" with "" to achieve the purpose of removing the string hollow lattice.

Str1= "  Zhang  three   "; System.out.println ("Go space =" +str1.replace ("", ""));

String substitution

A string substitution is a new string that replaces the string at the specified position in the original string, generates a new string, and can be implemented by means of replace () and Replacefirst ().

1.replace () method

This method is used to replace all strings that match the specified string.

Str.replace (String regex,string replacement)

The string to be replaced in the regex:str.

Replacement: A new string that is used to replace each regex.

Return value: Returns the replaced string.

// Find Replacements                 str= "string constant string constant";                System.out.println ("Find replacement =" +str.replace ("constant", "variable"));                System.out.println ("str=" +str);

2.replaceFirst ()

The method is used to replace the first occurrence of the specified string, and subsequent occurrences of the specified string are not replaced.

Str.replacefirst (String regex,string replacement)

The string to be replaced in the regex:str.

Replacement: A new string to replace the first regex.

Return value: Returns the replaced string.

// value to replace the first one                Str= "string constant string constant";        System.out.println ("Find replacement =" +str.replacefirst ("constant", "variable"));                 // Replacefirst (regular expression, "replaced content")                 Str.replaceall ("Regular expression", "");        

Note: The character of the regex to be replaced has strict requirements and needs to be consistent with the original string, otherwise it will not be successfully replaced.

Letter Case Conversion

The string class provides methods for letter-case conversions, namely the toLowerCase () and toUpperCase () methods.

1.toLowerCase () method

This method converts the uppercase letters in a string to lowercase letters, and if it turns out to be lowercase, it does not change, and finally returns a new string with the same length as the original character.

Str.tolowercase ()

STR: the string to be converted.

Return value: The full lowercase string.

2.toUpperCase () method

The method converts the lowercase letters in the string to uppercase, and if it is originally uppercase, does not change, and finally returns a new string with the same length as the metacharacters.

Str.touppercase ()

STR: the string to be converted.

Return value: Replace all uppercase strings.

// uppercase and lowercase conversions, only effective        for English letters str1= "Abcdegghhubhh";                System.out.println ("turn into uppercase:" +str1.touppercase ());                System.out.println ("turns into lowercase:" +str1.tolowercase ());

Description: When using the toLowerCase () and toUpperCase () methods for case conversion, numbers or non-characters are not affected, only English letters are effective.

String segmentation

The split () method splits the string according to the specified delimiter and stores the split result in a string array. The split () method provides two types of weight in the form.

1.split (String sign) method

The method completely splits the string according to the specified delimiter.

Str.split (String sign)

Sign: Splits the delimiter of a string, or you can use a regular expression.

Return value: The string separated by the delimiter.

// string Delimited        str2= "Name ~21~ man ~ Address ~123456";                String[]a=str2.split ("~");                 int i=1;                  for (String t:a)        {            System.out.print (i+ ":" +t);                        I+ +;        }

Strings String 2

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.