Skip to content

Commit 028cb88

Browse files
committed
Add English translations for String data structure
1 parent ed1d08f commit 028cb88

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

en/basics_data_structure/string.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# String
2+
3+
String-related problems often appear in interview questions. In actual
4+
development, strings are also frequently used. Summarized here are common uses
5+
of strings in C++, Java, and Python.
6+
7+
## Python
8+
9+
```python
10+
s1 = str()
11+
# in python, `''` and `""` are the same
12+
s2 = "shaunwei" # 'shaunwei'
13+
s2len = len(s2)
14+
# last 3 chars
15+
s2[-3:] # wei
16+
s2[5:8] # wei
17+
s3 = s2[:5] # shaun
18+
s3 += 'wei' # return 'shaunwei'
19+
# list in python is same as ArrayList in java
20+
s2list = list(s3)
21+
# string at index 4
22+
s2[4] # 'n'
23+
# find index at first
24+
s2.index('w') # return 5, if not found, throw ValueError
25+
s2.find('w') # return 5, if not found, return -1
26+
```
27+
28+
In Python, there's no StringBuffer or StringBuilder. However, string manipulations
29+
are fairly efficient already.
30+
31+
## Java
32+
33+
```java
34+
String s1 = new String();
35+
String s2 = "billryan";
36+
int s2Len = s2.length();
37+
s2.substring(4, 8); // return "ryan"
38+
StringBuilder s3 = new StringBuilder(s2.substring(4, 8));
39+
s3.append("bill");
40+
String s2New = s3.toString(); // return "ryanbill"
41+
// convert String to char array
42+
char[] s2Char = s2.toCharArray();
43+
// char at index 4
44+
char ch = s2.charAt(4); // return 'r'
45+
// find index at first
46+
int index = s2.indexOf('r'); // return 4. if not found, return -1
47+
```
48+
49+
The difference between StringBuffer and StringBuilder is that the former guarantees
50+
thread safety. In a single-threaded environment, StringBuilder is more efficient.

0 commit comments

Comments
 (0)