Common Operations on strings)

Source: Internet
Author: User
Indexof-search function
Int string. indexof (string value );
Returns the position of the first occurrence of a substring in a string, starting from 0. If no substring is found,-1 is returned.
Int string. indexof (string value, int startindex );
Same as above, but searches from the startindex position of the string until the end of the string.
Int string. indexof (string value, int startindex, int count );
Same as above, but starts from the specified position of the string and searches back for Count characters.
Note that the values of the last two parameters must meet the following conditions:
0 <= startindex <Length
0 <count <= length-startindex
Example: String mystr = "abcabcabc ";
Expression Return Value
Mystr. indexof ("") -1 // case sensitive
Mystr. indexof ("") 0
Mystr. indexof ("A", 0) 0 // the index of the first character is 0
Mystr. indexof ("A", 1) 3
Mystr. indexof ("A", 1, 2) -1
Mystr. indexof ("A", 1, 3) 3 // search for a total of Count characters

Lastindexof-reverse search function
Returns the position of the last occurrence of the substring in the string, from the back to the front.
0 <= startindex <Length
0 <count <= startindex + 1

Insert-insert Function
String insert (INT startindex, string value );
Insert a string at the specified index position of the string.
In the returned string, the index number of the first character of the value string is startindex.

Remove-delete a function
String remove (INT startindex, int count );
Deletes a specified number of characters from the specified index position of a string.
The index and Count must reference the position in the string, startindex + count <= Length

Replace-replace Function
String Replace (char oldchar, char newchar );
String Replace (string oldvalue, string newvalue );

Concat-connection Function
String Concat (parameter );
Parameters can be objects, object arrays, strings, and string arrays. Multiple objects (strings) are separated by commas (,).

Join-concatenation Function
String join (string separator, string [] value );
Concatenates the specified separator between each element of the string array to generate a single concatenation string.
String join (string separator, string [] value, int startindex, int count );
Concatenates some array elements, starting from the startindex element, and concatenates count elements.

Split-split Function
String [] Split (Params char [] separator );
Splits the string into substrings Based on the delimiter, and returns the result as a string array.
String [] Split (char [] separator, int count );
The Count parameter specifies the maximum number of returned array elements for partial segmentation.
Example: String mystr = "1 + 2 + 3-4 + 5 + 6 ";
Expression Return
Mystr. Split ('-'); {"1 + 2 + 3", "4 + 5 + 6 "}
Mystr. Split ("+-". tochararray ()); {"1", "2", "3", "4", "5", "6 "}
Mystr. Split (New char [] {'+ ','-'}); {"1", "2", "3", "4", "5", "6 "}
Mystr. Split ("+-". tochararray (), 4 ); {"1", "2", "3", "4 + 5 + 6 "}
MySQL. Split ("+-". tochararray (), 100 ); {"1", "2", "3", "4", "5", "6 "}
Note the usage of the delimiter
At most count elements are returned for partial splitting.

tochararray-scatter function (Haha, I borrowed the term in Flash)
char [] tochararray ();
copy the characters in the string to the character array.
char [] tochararray (INT startindex, int length);
copy the characters in the specified substring to the character array.
example: String mystr = "diffmaker";
Expression return
mystr. tochararray (); {'D', 'I', 'F', 'F', 'M', 'A', 'k ', 'E', 'R'}
mystr. tochararray (4, 4); {'M', 'A', 'k', 'E'}

Trim | trimstart | trimend-trim Function
String trim (); // remove the leading and trailing white spaces (including Chinese and English spaces) from the string)
String trimstart (); // removes the string header space (including Chinese and English spaces)
String trimend (); // Remove trailing white spaces (including Chinese and English spaces) from the string)
String trim (Params char [] trimchars); // remove the first and last characters of the string
String trimstart (Params char [] trimchars); // remove the first character of the string
String trimend (Params char [] trimchars); // remove the tail character of the string
When no parameter is specified, blank characters are removed.
When a parameter is specified, the specified character is removed.

Startswith | endswith-endpoint Function
Bool startswith (string value); // checks whether the string starts with a substring.
Bool endswith (string value); // checks whether the string ends with a substring.

Padleft | padright-Fill function
String padleft (INT totalwidth); // Add a space on the left side of the string to reach the specified length.
String padright (INT totalwidth); // Add a space to the right of the string to reach the specified length
String padleft (INT totalwidth, char paddingchar); // Add the specified character to the length on the left
String padright (INT totalwidth, char paddingchar); // Add the specified character to the right to the fixed length

Substring-subfunction
String substring (INT startindex); // starts from the specified character position to the end of the string
String substring (INT startindex, int length); // obtain the specified length from the specified character position
Startindex starts from scratch
If startindex is equal to the length of the string, return: String. Empty
Startindex + count <= Length

Other simple functions
String. tolower (); // converts the string to lowercase.
String. toupper (); // convert to uppercase

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.