Lesson 15 — Common Methods of Strings (1)


0x00 Review and opening

It took two previous articles to introduce Rust’s strings and understand the connections and differences between them. This lesson begins with the introduction of the common methods of modifying, adding, and deleting the Rust string.

0x01 Append (Push)

You can append a single character, char, to the end of a string using the push() method, or you can append a string literal using the push_str() method. Both methods append to an existing string and do not return a new string. Since the string append operation modifies the original string, the string must be mutable. That is, string variables must be modified by the MUT keyword.

The example code is as follows:

let mut string = String::from("hello "); string.push('r'); println! (" push() -> {}", string); string.push_str("ust!" ); println! (" append string push_str() -> {}", string);Copy the code

Code run result:

Push () -> hello r append string push_str() -> hello rust!Copy the code

0x02 Insert (Insert)

You can insert a single character, char, using the insert() method, or you can insert string literals using the insert_str() method. Unlike the push() method, these methods require passing two arguments, the first argument being the index of where the character (string) is inserted, and the second argument being the character (string) to be inserted. The index counts from 0, and an error occurs if the index is out of bounds. Since string insertion modifiers the original string, the string must be mutable. That is, string variables must be modified by the MUT keyword.

The example code is as follows:

let mut insert_string = String::from("hello rust!" ); insert_string.insert(5, ','); println! (" insert() -> {}", insert_string); insert_string.insert_str(6, " I like"); println! (" insert_str() -> {}", insert_string);Copy the code

Code run result:

Insert () -> hello, rust! Insert string insert_str() -> hello, I like rust!Copy the code

0x03 Connection (Catenate)

1, use,+or+ =Connection string

Concatenating strings with + or += requires that the argument on the right be of the string’s Slice reference type. When you call the + operator, you are calling the Add method in the String library.

The source code is as follows:

#[inline] fn add(mut self, other: &str) -> String { self.push_str(other); self }

Here the second argument to the add method is a reference type. So we are using + and must pass the slice reference type. You cannot pass String directly. Both + and += return a new string. So variable declarations can be made without the MUT keyword.

The example code is as follows:

let string_append = String::from("hello "); let string_rust = String::from("rust"); // &string_rust is automatically dereferenced to &str let result = string_append + &string_rust; let mut result = result + "!" ; result += "!!!" ; println! (" connection string + -> {}", result);Copy the code

Code run result:

Connection string + -> Hello rust!!!!Copy the code

See here, if anyone has any questions.

* push_str (); * push_str (); * push_str (); * push_str (); * push_str (); Isn’t it unnecessary?

A: Why don’t we just print string_append? If you print string_append, you’re going to get desperate, because the compiler is going to make the following error.

println! (” output string_append -> {}”, string_append); | ^^^^^^^^^^^^^ value borrowed here after move

Literal translation: This value is used after being moved. Just to explain, the string_append variable is transferred to the add method by calling the add method, and the add method is released, and the string_append variable is released. Using string_append any more will cause an error. Just understand, here involves the transfer of ownership (Move) related knowledge. Just to know, I’ll bury a hole here, and the ownership will be explained in detail.

2, use format! Connection string

format! This applies to strings and &str. format! Print! “, which was discussed in detail earlier in lesson 9 — Inputs and Outputs.

The example code is as follows:

let s1 = "hello"; let s2 = String::from("rust"); let s = format! (" {} {}!" , s1, s2); println! ("{}", s);Copy the code

Code run result:

hello rust!
Copy the code

0x04 Replace

If you want to replace one of the strings with another, use the replace() method. There are three methods associated with substitution.

1, the replace

This method applies to types String and &str. The replace method takes two arguments, the first is the string to be replaced, and the second is the new string. This method replaces all matched strings. This method returns a new string instead of manipulating the original string.

The example code is as follows:

// replacen let string_replace = "I like rust. Learning rust is my favorite!" ; let new_string_replace = string_replace.replace("rust", "RUST"); dbg! (new_string_replace);Copy the code

Code run result:

new_string_replace = "I like RUST. Learning RUST is my favorite!"
Copy the code
2, replacen

This method applies to types String and &str. The replacen method takes three arguments. The first two arguments are the same as the replace method, and the third argument represents the number of replacements. This method returns a new string instead of manipulating the original string.

The example code is as follows:

// replacen let string_replacen = "I like rust. Learning rust is my favorite!" ; let new_string_replacen = string_replacen.replacen("rust", "RUST", 1); dbg! (new_string_replacen);Copy the code

Code run result:

new_string_replacen = "I like RUST. Learning rust is my favorite!"
Copy the code
3, replace_range

This method applies only to strings. Replace_range takes two arguments, the first is the Range of the string to be replaced, and the second is the new string. This method directly operates on the original string and does not return the new string. This method needs to be modified with the MUT keyword.

let mut string_replace_range = String::from("I like rust!" ); string_replace_range.replace_range(7.. 8, "R"); dbg! (string_replace_range);Copy the code

Code run result:

string_replace_range = "I like Rust!"
Copy the code

0x05 Delete (Delete)

There are four methods related to string deletion, they are POP, remove, TRUNCate, and clear. These four methods apply only to strings.

Pop — deletes and returns the last character of the string.

This method directly manipulates the original string. But there is a return value, which is an Option type, or None if the string is empty. The Option type is covered in a subsequent section.

The example code is as follows:

Let mut string_pop = String::from("rust pop Chinese!" ); let p1 = string_pop.pop(); let p2 = string_pop.pop(); dbg! (p1); dbg! (p2); dbg! (string_pop);Copy the code

Code run result:

p1 = Some( '! ',) p2 = Some(' text ',) string_pop = "rust pop"Copy the code
2, remove — remove and return the character at the specified position in the string

This method directly manipulates the original string. But there is a return value, and the string whose return value is the deletion position receives only one argument, indicating the starting index position of the character. The remove method handles strings in bytes, and an error will occur if the position given by the argument is not a valid character boundary.

  • What is an illegal character boundary?

    In Rust, an English character or Arabic number takes up one byte, and a Chinese character takes up three strings. You can use the STD ::mem::size_of_val() method to see how many bytes are in memory. Other emojis took up to four bytes.

    Why, you may ask? This has to do with THE UTF-8 encoding, which I won’t explain here, but if I have time later, I can pull up an article to explain Unicode background. If you want to know the answer, you can also search the Internet.

    The example code is as follows:

    Let word = "in "; let ch = "1"; println! ("word is {} bytes ", STD ::mem::size_of_val(word)); println! (STD ::mem::size_of_val(ch));Copy the code

    Code run result:

    Word is 3 bytes. Ch is 1 byteCopy the code

After the above example, maybe you understand what is an illegal character boundary. In the following code example, string_remove is a String whose length is calculated to be 18 bytes according to the above rules. If you want to delete the first character, pass the parameter 0. If you want to delete the second character directly, you need to pass parameter 3. If you pass an argument of 1 or 2, you will get an invalid character boundary error.

The example code is as follows:

Let mut string_remove = String::from(" test the remove method "); println! ("string_remove takes {} bytes ", STD ::mem::size_of_val(string_remove.as_str())); // delete the first character string_remove.remove(0); // string_remove.remove(1); // string_remove.remove(3); dbg! (string_remove);Copy the code

Code run result:

String_remove is 18 bytes string_remove = "Try rmove"Copy the code
Truncate — deletes all characters from the specified position in the string.

This method directly manipulates the original string. No return value. The truncate method processes the string in bytes, and an error occurs if the position given by the argument is not a valid character boundary.

The example code is as follows:

Let mut string_truncate = String::from(" test truncate"); string_truncate.truncate(3); dbg! (string_truncate);Copy the code

Code run result:

String_truncate = "measure"Copy the code
4. Clear — Clear the string

This method directly manipulates the original string. After the truncate method is invoked, all characters in the string are deleted, equivalent to when the truncate method parameter is 0.

The example code is as follows:

let mut string_clear = String::from("string clear"); string_clear.clear(); dbg! (string_clear);Copy the code

Code run result:

string_clear = ""
Copy the code

0 x06 summary

This paper briefly introduces the common modification methods of string, which are divided into five parts: append, insert, connect, replace and delete. The next section continues with the common method of string access.

0x04 Source code for this section

015 · StudyRust – Code Cloud – Open Source China (gitee.com)

The next section looks at the common methods (2) for strings.