Deep parsing of C + + and Java string _c languages

Source: Internet
Author: User
Tags stringbuffer

All string classes originate from the C-language string, while the C-language string is an array of characters. There are no strings in the C language, only character arrays.
To talk about C + + string: C + + provides two kinds of string representation: The style of the string and the type of the strings introduced by Standard + +. It is generally recommended to use the string type, but in practice it is still the case that the old C-style string is used.
1.C-style string:C-style strings originate from C and are extended in C + +. The string is stored in a character array, for example:
const char *STR = "Zhangdan"; (Don't forget the last one)
This represents a string with a constant character array. You can manipulate the string as long as you manipulate the pointer. Such as:
const char * str = "Zhangdan"; const char *p = str; Then it's OK to do the operation on p.
2. String type of standard C + +:If you use it, you first need to introduce the header file: #include <string>
The standard string types provided in C + + provide the following actions:
(1). Supports initializing a string object with a sequence of characters or a second string. C-style strings do not support the initialization of another string with another string.
(2). Copy,c style strings that support strings are implemented through the strcpy () function.
(3). Supports read-write access to individual characters. For C-style strings, only a single character can be accessed by a dereference or by a subscript operation.
(4). Supports two string equality comparisons, and for C-style strings, comparisons are done through the strcmp () function.
(5). Support two string connections, for C-style strings, copy the strcpy () function to a new instance, and then use Strcat () to pick up two strings. Such as:
String str1 = "111111", str2 = "222222";
String STR3 = str1 + str2;
(6). Support for string length queries: string s ("XXXXXXX"); Str.size () is the length of the string.
Reciprocal conversion: const char * str = STR2.C_STR (); You cannot assign a string type directly to a character array, but you can assign a character array to a string type: For example: const char *STR = "Zhangdan"; string str2 = str;

Input of C + + string class
(1) method one: and C string Input method is the same.
(2) Method Two: Use the Getline function.
Example: String A;
Getline (Cin,a);

Conversion of strings to numbers
the Atoi function gets a c string parameter that returns the corresponding int value. If the argument does not correspond to an int value, the Atoi returns 0. The Atoi function is in the library where the file is cstdlib. If the number is too large to be converted to a value of type int, you can use ATOL to convert the string to a long type value.
For example:
Atoi ("1234"); return integer 1234
Atoi ("#123"); return 0
The common method of string in C + + is not introduced, introduce several commonly used: Begin (), End (), append (), and so on.
2:string in Java: in Java, string is not part of the base type in 8, so string is an object and the default value is NULL
For example: string str = new string (); and string str = new String (""); it means to construct an empty string (to understand the difference between null and "").
Look at the following code:

String str = "xxx"; 
String str2 = new string ("xxx");
System.out.println (str = = str2);
System.out.println (Str.equals (str2)); 


The result:
False
True

Why, then? in Java, = = is a comparison of the address, and equals is the content of the comparison, why not the same address?
The concept of a constant pool is introduced first:

Chang (constant pool) refers to some data that is determined at compile time and is saved in the compiled. class file. It includes constants in classes, methods, interfaces, and so on, as well as string constants.
When we assign a string to a string variable, such as String str = "XXXX"; At this point, go to the constant pool to find there is no "xxxx" string copy, if any, the address of STR to the constant pool in the string constant "xxxx" address, if not the constant pool in the set up "XXXX" string constants. The new String ("XXXX") is placed in the heap memory and has its own memory space. So the address is different when compared.
Look at the following code:

String str = "Zhang"; 
String str2 = "Peng"; 
String STR3 = "Zhangpeng"; 
String STR4 = "Zhangpeng" 
str = str2; 
System.out.println (str = = STR3); 
System.out.println (STR3 = = STR4)

The results are:
True
True

Why, then?
First, we need to know that Java ensures that there is only one copy of a string constant.

Because the "Zhangpeng" in the example STR3 and STR4 are string constants, they are determined at compile time, so STR3==STR4 is true, and "Zhang" and "Peng" are string constants, and when a string is concatenated by multiple string constants, It is definitely also a string constant, so str2 is also parsed as a string constant at compile time, so str2 is also a reference to "Zhangpeng" in a constant pool.

The difference between string and StringBuffer in JAVA:

String:
Is that the object is not the original type.
is an immutable object, and once created, it cannot modify its value.
The modification of a string object that already exists is to re-create a new object and then save the new value in.
String is the final class, which cannot be inherited.
StringBuffer:
is a mutable object that, when modified, does not re-establish objects like string
It can only be built through constructors,
StringBuffer sb = new StringBuffer ();
He cannot be paid by the value sign.
SB = "XXXXX";
After the object is established, the memory space is allocated in memory and a null is initially saved. to StringBuffer
The Append method can be used when the value is paid.
Sb.append ("Hello");
StringBuffer is more efficient in string concatenation operations than string:
String str = new string ("xxx");
str = "XX";
The processing step is actually by creating a stringbuffer and then calling append (), and finally
Then the StringBuffer tosting ();
In this case, the string concatenation operation is more than StringBuffer, so it's slow

Ask a question: why is stringbuffer so efficient that we still need string?
No, check it out. The direction is a constant pool.

Python string:
Python is a powerful scripting language that defines a string without defining a type. Python strings usually have single quotes (' ... '), double quotation marks ("..."), three quotation marks ("" "" ""), or (' ... ') are surrounded by a string of three quotes that can be composed of multiple lines, typically representing large segments of descriptive strings. There is basically no difference when used, but double quotes and triple quotes ("" "") can contain single quotes, and triple quotes (' ... ') can contain double quotes without escaping. You can use ' \ ' When special escape is required
Python also has a number of functions for string manipulation. Specific can dir a, with C + + and Java are similar.

The above is the entire content of this article, I hope to help you learn.

Related Article

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.