Data-Structure String

1. String Representation

A string can be represented as a sequence of characters, where each character occupies a specific position known as an index. Indices start at 0 and increment sequentially. For example, the string "Hello" consists of the character 'H' (index 0), 'e' (index 1), 'l' (index 2), 'l' (index 3), and 'o' (index 4).

2. String Length

The length of a string refers to the number of characters it contains. The length can be obtained using specific functions or properties. For example, for the string "Hello", the length is 5.

3. String Concatenation

String concatenation is the operation of merging two or more strings into a single string. In many programming languages, the addition (+) operator can be used to concatenate strings. For example, the result of "Hello" + "World" is "HelloWorld".

4. Substring

A substring is a portion of a string extracted from a larger string. Substrings can be retrieved based on indices or specified start and end positions. For example, for the string "HelloWorld", the substring "Hello" can be obtained using indices 0 to 4 (excluding 4).

5. String Comparison

Strings can be compared in lexicographical order. Common comparison operations include equality checks and case-sensitive/case-insensitive comparisons.

6. String Search and Replace

Specific substrings can be searched within a string, and they can be replaced or modified. These operations are useful for searching and modifying text data.

7. String Splitting and Joining

A string can be split into multiple substrings, or multiple strings can be joined into a single large string. Splitting and joining operations are frequently used for parsing and processing text data.

8. String Formatting

String formatting is the process of inserting variables or values into predefined string templates. This is very useful when generating dynamic text or constructing complex output.

9. String Operations

Strings also support many other operations, such as character searching, character replacement, case conversion, and removing whitespace.