Common methods for handling strings

Source: Internet
Author: User

I'm learning C # and JS so it's the string handling method in this context:
Javascript:::::::::

Creation of strings
There are several ways to create a string. The simplest is to enclose a set of characters in quotation marks, which can be
Assigns a value to a string variable.
var mystr = "Hello, string!";
, but in essence, they are not real string objects, they are exactly the values of the string type. To create
To build a string object, use the following statement: var strobj = new String ("Hello, string!");
Using the typeof operator view, it is found that the MyStr type above is string and the Strobj type is
Object
If you want to know the length of the string, use its Length property: String.Length.
Gets the character used by the specified position of the string: String.charat (index);


concat () – combines two or more characters of text to return a new string.
indexof () – returns the index at the first occurrence of a substring in a string. If there is no match, return-1

charat () – returns the character at the specified position.
LastIndexOf () – Returns the index of the last occurrence of a substring in a string, if there is no match.
Returns-1.
Match () – checks whether a string matches a regular expression.
substring () – Returns a substring of the string. Incoming parameters are the start and end positions.
replace () – Used to find a string that matches a regular expression, and then use the new string instead of the match
The string.
Search () – performs a regular expression matching lookup. If the lookup succeeds, the string that matches the cable is returned
An index value. otherwise returns-1.
Slice () – Extracts part of a string and returns a new string.
Split () – A string is made into a string array by dividing the string into substrings.
Length () – Returns the length of a string, the length of which is the number of characters it contains.
toLowerCase () – Converts the entire string into lowercase letters.
toUpperCase () – turns the entire string into uppercase.

To add a line break to a string, you need to use the escape character "\ n":

Get a copy of a part of a string:
String.substring (from, to)
The first parameter from specifies the starting position of the substring in the original string, based on the 0 index, and the second
The parameter to is optional, which specifies the substring at the end of the original string (based on the 0 index), in general
, it should be larger than the from, and if it is omitted, then the substring will remain at the end of the original string.
What if the parameter from is accidentally larger than the parameter? JavaScript automatically mediates the substring
In other words, substring () always starts with the smaller of the two parameters and ends with the larger one.
。 Note, however, that it contains the character at the beginning of the position, but does not contain that character at the end position.


The prototype for Slice () is: String.slice (start, end)
The argument start indicates the starting position of the substring, and if it is a negative number, it can be understood as the beginning of the countdown.
For example, 3 means starting from the last third; The parameter end indicates the end position, as with start, it can also be negative
, and its meaning is also expressed to the end of the penultimate part. The slice () parameter can be negative, so it is more than substring ()
More flexible, but less tolerant, if start is larger than end, it will return an empty string (example slightly).


Another method is substr (), whose prototype is: String.substr (start, length)
From the prototype can be seen the meaning of its parameters, start represents the starting position, length is the substring of the
Length. The JavaScript standard does not advocate the use of this method.


Use the toLowerCase () and toUpperCase () methods:
var city = "Shanghai";
City = City.tolowercase (); City was "Shanghai" now.

Problem:
Determines whether a string contains another string.
Solution:
Use the IndexOf () method of the string:
Strobj.indexof (substring[, StartIndex])
Strobj is the string to be judged, substring is the substring to find in Strobj,
startindex is optional, indicating the starting position of the lookup (based on the 0 index), and if startindex omitted, the
Strobj start Lookup, if startindex is less than 0, start at 0 if startindex is greater than the maximum index
, start at the maximum index.

string manipulation in C #
myString = Mystring.tolower (); Convert all characters to lowercase
myString = Mystring.toupper (); Convert all characters to uppercase
myString = Mystring.trim (); Also delete spaces before and after a string
Char[] TrimChars = {', ' e ', ' s '}; Ready to delete characters

myString = Mystring.trim (trimChars); Remove all specifiers
myString = Mystring.trimend (); Whitespace after deleting a string
myString = Mystring.trimstart (); Remove spaces before a string
myString = Mystring.padright (14, "); When the string is not 14 bits long, it is in his right
Edge fills with specified characters

myString = Mystring.padleft (14, "); When the string length is not enough for 14 bits, in his left
Edge fills with specified characters
* Split String *
string[] Nstrs = Mystring.split (", 3); Split by Space and return to the top three
A string

* Get sub-string *

String a = Mystring.substring (2,2); Getting from the third bit of the mystring string
Two characters because the index
The starting bit is 0

String a = Mystring.replace ("i", "O"); Replace All "I" in this string with "
O
The string in C # is actually a read-only group of char variables.

CompareOrdinal compares two string objects by calculating the numeric value of the corresponding Char object in each string

CompareTo is compared with the specified object or string and returns an indication of their relative values
Concat a string representation of one or more instances of a string, or the value of one or more instances of object
Form

Contains returns a value that indicates whether the specified string object appears in this string

Copy creates a new instance of a string with the same value as the specified string
CopyTo copies a specified number of characters from a specified location to a specified position in a Unicode character array
EndsWith determines whether the end of a string matches the specified string
Equals determines whether two string objects have the same value

Format
Replaces each format item in the specified string with the text equivalent of the value of the corresponding object

GetEnumerator retrieves an object that can iterate through each character in this string
GetHashCode returns the hash code of the string
GetTypeCode returns the TypeCode of a class string

IndexOf reports the index of the first occurrence of a string or one or more characters in this string

IndexOfAny Report specifies the index of the first occurrence of any character in this instance of a Unicode character array
Insert inserts a specified string instance at the specified index location
Intern retrieving a reference to a specified string by the system
Isinterned retrieving a reference to a specified string

IsNormalized
Indicates whether this string conforms to a specific Unicode paradigm

IsNullOrEmpty indicates whether the specified string object is a null reference or an empty string
LastIndexOf reports the index of the last occurrence of the specified Unicode character or string in this instance
Position

Lastindexofany
Reports that one or more characters specified in a Unicode array are the most in this instance
After a
The index position of a match
Op_equality
Determines whether two specified string objects have the same value

Common methods for handling strings

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.