Analysis of C ++ and JAVA strings

Source: Internet
Author: User
Tags string to number

All string classes originate from C strings, while C strings are character arrays. The C language does not contain strings, but only character arrays.

Let's talk about the C ++ string: C ++ provides two types of strings: C-style strings and string types introduced by standard C ++. It is generally recommended to use the string type, but the old-fashioned C-style string should be used in actual situations.

1. C-style string: The C-style string originated from C and is extended in C ++. Strings are stored in a character array, for example:

Const char * str = "zhangdan"; Do not forget the last \ 0)

Here, a constant character array is used to represent a string. You only need to operate the pointer when operating the string. For example:

Const char * str = "zhangdan"; const char * p = str; then you can operate on p.

2. string type of standard C ++: If it is used, first introduce the header file: # include <string>

The standard string type provided in C ++ provides the following operations:

(1). A String object can be initialized using a character sequence or a second string. C-style strings do not support initializing another string with another string.

(2). Supports copy between strings. C-style strings are implemented through the strcpy () function.

(3). Supports read/write access to a single character. For C-style strings, a single character can be accessed only by unreferencing or subscript operations.

(4). Two strings are supported for equal comparison. for C-style strings, comparison is implemented through the strcmp () function.

(5). Supports two string connections. For a C-style string, use the strcpy () function to copy it to a new instance, and then use strcat () to connect the two strings. For example:

String str1 = "111111", str2 = "222222 ";

String str3 = str1 + str2;

(6). query of the string length is supported: string s ("XXXXXXX"); str. size () is the length of the string.

Mutual conversion: const char * str = str2.c _ str (); // The string type cannot be directly assigned to the character array, but a character array can be assigned to a string type: for example: const char * str = "zhangdan"; string str2 = str;

C ++ string type input
(1) Method 1: The same as the C string input method.
(2) Method 2: Use the getline function.
Example: string;
Getline (cin, );

String to number Conversion
The atoi function gets a C string parameter and returns the corresponding int value. If the parameter does not match an int value, 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 an int value, you can use atol to convert the string to a long value.
For example:
Atoi ("1234"); // returns an integer of 1234.
Atoi ("#123"); // return 0

Common Methods of strings in C ++ are not described one by one. Several common methods are introduced: begin (), end (), append (), and so on.
2: String in JAVA: In JAVA, String does not belong to the basic type of 8. Therefore, String is an object and the default value is null.
For example, String str = new String (); and String str = new String (""); mean to construct an empty String and understand the difference between null and ).

See the following code:

 

 
 
  1. String str = "xxx";  
  2. String str2 = new String("xxx");
  3. System.out.println(str == str2);
  4. System.out.println(str.equals(str2)); 

The result is:

False

True

Why? In JAVA, = is a comparison of the address, while equals is a comparison of the content. Why is the address different?

First introduce the concept of constant pool:
The constant pool refers to the data that is identified during the compilation period and saved in the compiled. class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.

When a String is assigned to a String variable, such as String str = "xxxx", check whether there is a copy of the "xxxx" String in the constant pool. If yes, point the str address to the address of the String constant "xxxx" in the constant pool. If not, create a String constant "xxxx" in the constant pool. While new String ("xxxx") is stored in heap memory and has its own memory space. Therefore, the address comparison is different.

See the following code:
 

 
 
  1. String str = "zhang";  
  2. String str2 = "peng";  
  3. String str3 = "zhangpeng";
  4. String str4 = "zhangpeng" 
  5. str += str2;  
  6. System.out.println(str == str3); 
  7. System.out.println(str3 == str4)

Result:

True

True

Why?

First, we need to know that Java will ensure that a String constant has only one copy.

Because the "zhangpeng" in str3 and str4 In the example are both string constants, they are determined during the compilation period, so str3 = str4 is true; both "zhang" and "peng" are string constants. When a string is connected by multiple string constants, it must be a String constant, therefore, str2 is also parsed as a String constant during compilation, so str2 is also a reference of "zhangpeng" in the constant pool.

Differences between String and StringBuffer in JAVA:

String:
Yes. The object is not of the original type.
An unchangeable object. Once created, its value cannot be modified.
To modify an existing String object, create a new object and save the new value.
String is a final class, which cannot be inherited.

StringBuffer:
It is a mutable object. objects are not re-created as strings when modified.
It can only be created by constructors,
StringBuffer sb = new StringBuffer ();
He cannot be paid by a value symbol.
Sb = "xxxxx ";

After the object is created, the memory space is allocated in the memory, and a null is initially saved to StringBuffer.
You can use its append method to pay a value.
Sb. append ("hello ");

StringBuffer is more efficient than String in String connection operations:

String str = new String ("xxx ");
Str + = "xx ";
The process is actually by creating a StringBuffer, then calling append (), and finally
Then convert StringBuffer toSting ();
In this case, the String connection operation is slower than the StringBuffer operation.

Q: Why is the StringBuffer efficiency so high? Do we need a String?

No, check it. The direction is the constant pool.

Python string:

Python is a powerful scripting language that defines strings without defining types. Python strings usually have single quotes '... '), double quotation marks "... "), three quotation marks """... ") or '''... '''). A string enclosed by three quotation marks can be composed of multiple rows. Generally, it can represent a narrative string of a large segment. There is basically no difference in use, but double quotation marks and three quotation marks """... ") can contain single quotes, three quotes ('''... ''') can contain double quotation marks without escaping. You can use '\' when special escape is required '\'

Python also has many string-operated functions. You can use dir, which is similar to C ++ and java.

 

This article from the "shenxiang Ming Dynasty selling apricot flowers" blog, please be sure to keep this source http://padden.blog.51cto.com/2514314/489043

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.