-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.rs
174 lines (134 loc) · 4.78 KB
/
string.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* String Literal
* Also known as String Slices
* (&str)
* String literals are used when the value of a string is known at compile time.
* String literals are hardcoded into a variable.
*/
fn str_literal() {
let name: &str = "John Doe";
let mail: &str = "[email protected]";
// String literals are static by default.
// This means that string literals are guaranteed to be valid for the duration of the entire program.
// We can also explicitly specify the variable as static
// println!("Name : {} Mail : {}", name, mail);
println!("Name : {}", name);
println!("Mail : {}", mail);
// We can also explicitly specify the variable as static by using &'static str
// (&'static str)
let address:&'static str = "New York";
println!("Address : {}", address);
}
/**
* String Object
* The String object type is provided in Standard Library.
* Unlike string literal, the string object type is not a part of the core language.
* String is a growable collection. It is mutable and UTF-8 encoded type.
* String object is allocated in the heap.
*/
fn str_object() {
// To create a String object, we can use any of the following
// String::new()
// String::from()
// String::new() create an empty string.
let blood_type = String::new();
// String::from() creates a string with some default value passed as parameter.
let company = String::from("DarK MatteR");
// printing the value of both the company and blood_type.
println!("{}", blood_type.len());
println!("{}", company);
// We also create a mutable String object and psuh the value later.
let mut designation = String::new();
// push_str push value to a String object
designation.push_str("Senior Engineer");
// Print the value of designation.
println!("{}", designation);
}
/**
* String Function.
*
*/
fn str_func() {
// new()
// Create an empty string object.
let new = String::new();
println!("Value is Empyt : {}", new); // Print the value of new.
// to_string()
// Convert a string literal to object type.
let to_str = "Hallo World!";
println!("{}", to_str.to_string());
// replace()
// Replace word in string.
// The replace() function takes two parameters.
// the first parameter is a string pattern to search for and the second parameter is the new value to be replaced.
let replace = "Hallo World!";
let replace = replace.replace("Hallo", "Oh, Hi");
println!("{}", replace);
// as_str()
// Extracts a string slice containing the entire string.
let as_str = String::from("Example String");
println!("{}", as_str.as_str());
// push()
// Appends the given char to the end of this String.
// Only apply to string object.
let mut push = "Rust Tutorial".to_string();
push.push('s');
println!("{}", push);
// let mut test = String::new();
// test.push('X');
// println!("{}", test);
// push_str()
// Appends a given string slice onto the end of a String.
// Only apply to string object.
let mut push_str = "Rust Tutorial".to_string();
push_str.push_str(", Python Tutorial");
println!("{}", push_str);
// len()
// Returns the total number of characters in a string (including spaces).
let len = "I'm learning Rust";
println!("{}", len.len());
// trim()
// Removes leading and trailing spaces in a string.
// NOTE : that this function will not remove the inline spaces.
let trim = " Let's test trim function \r\n";
println!("Before trim : {}", trim);
println!("length is : {}",trim.len());
println!("After trim : {}", trim.trim());
println!("length is after trim : {}",trim.trim().len());
// split_whitespace()
// Splits the input string into different strings.
let split_whitespace = "Rust is preatty awesome!".to_string();
// Loop through ther string.
let mut i = 1;
// for loop.
for token in split_whitespace.split_whitespace() {
println!("Token : {} {}", i, token);
i += 1;
}
// split()
// Returns an iterator over substrings of a string slice, separated by characters matched by a pattern.
let split = "PHP, Python, C++, Javascript, Rust, Ruby";
// for loop.
for name in split.split(",") {
println!("Skill : {}", name);
}
// chars()
// Individual characters in a string can be accessed using the chars method.
let chars = "HELLO RUST!".to_string();
// For loop.
for char in chars.chars() {
println!("{}", char);
}
}
/**
* Main function
* Mamin function run all ther other function.
*/
fn main() {
// Example of String Literal.
str_literal();
// Example of String Object.
str_object();
// Example of String Function.
str_func();
}