You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 09-Data-Structure-Modern-Operators-and-Strings/README.md
+247-5
Original file line number
Diff line number
Diff line change
@@ -482,7 +482,7 @@ We an also use the `rest` on `DOM` elements, which allows us to do advanced func
482
482
rest.get(document.querySelector("h1"));
483
483
```
484
484
485
-
### Maps Iteration
485
+
# Maps Iteration
486
486
487
487
Elements are added to a `Map` using the `set()` method. But this can be cumbersome if you're trying to add multiple elements at the same time. To fix that, we can create a new `map` using an array inside the `map`.
488
488
@@ -579,7 +579,7 @@ Now there are other data structures not built into JavaScript like:
2. Use Sets when **high performance** is really important.
601
601
3. Use Sets to **remove duplicates** from arrays.
602
602
603
-
## Objects
603
+
### Objects
604
604
605
605
```js
606
606
consttask= {
@@ -615,7 +615,7 @@ const task = {
615
615
3. Use when you need to include `functions` (methods)
616
616
4. Use when working with `JSON` (can covert to `Map`)
617
617
618
-
## Maps
618
+
### Maps
619
619
620
620
```js
621
621
consttask=newMap([
@@ -631,3 +631,245 @@ const task = new Map([
631
631
4. Easy to compute size
632
632
5. Use when you simply need to map key to values
633
633
6. Use when you need keys that are not strings.
634
+
635
+
# Working with Strings - Part 1
636
+
637
+
In JavaScript we work a lot with strings and there are some important `string methods` worth knowing. This methods are very similar with the `array methods`
638
+
639
+
We can get elements in a string using the dot notation and the postion number. (Like arrays, strings are also zero-based)
640
+
641
+
```js
642
+
constairline="Dana Air Nigeria";
643
+
constplane="Boeing 700";
644
+
645
+
console.log(plane[0]); // output B
646
+
console.log(plane[1]); // output o
647
+
console.log(plane[2]); // output e
648
+
console.log("B737"[0]); // output B
649
+
```
650
+
651
+
We can also get the lenght of a string and the position of an element in a string.
console.log(airline.indexOf("Codo")); // output -1 (since result is not found)
660
+
```
661
+
662
+
### Slice Method
663
+
664
+
The `slice()` method returns a shallow copy of a portion of an arrayl or string into a new array object selected from `start` to `end`
665
+
666
+
Syntax
667
+
668
+
```js
669
+
slice();
670
+
slice(start);
671
+
slice(start, end);
672
+
```
673
+
674
+
The importance of using these indexes is using the `slice()` method to get `indexOf` of a string element or array.
675
+
676
+
**Start**
677
+
678
+
```js
679
+
console.log(airplane.slice(5));
680
+
```
681
+
682
+
This will slice the string `Dana Air Nigeria` by five positions and output the remaining result, which will be `Air Nigeria`
683
+
684
+
The result of a sliced string is referred to as a **sub string** because it does not change the original string. Of course this is because strings are immutable.
685
+
686
+
If you need to change the string, you would have to store it in a variable or data structure.
687
+
688
+
**End**
689
+
690
+
```js
691
+
console.log(airplane.slice(4, 7));
692
+
```
693
+
694
+
The result will be `Ai` because 4 will slice `Dana ` and 7 will slice `r Nigeria`
695
+
696
+
> **Note**
697
+
> The lenght of the extracted string(sub string) will always be `start - end`. In the example above `7-4=2` hence the result(Ai)
698
+
699
+
In real world example, we may not know the string we wish to extract. So how do you extract a string if we didn't know what it is? We use the `indexOf()` method.
- `toLowerCase()`: Converts all the alphabetic characters in a string to lowercase.
865
+
- `toUpperCase()`: Converts all the alphabetic characters in a string to uppercase.
866
+
- `trim()`: Removes the leading and trailing white space and line terminator characters from a string
867
+
- `replace()`: Replaces text in a string, using a regular expression or search string.
868
+
- `replaceAll()`: Replaces all occurence of text in a string, using a regular expression or search string.
869
+
- `indexOf()`: Returns the position of the first occurrence of a substring.
870
+
- `lastIndexOf()`: Returns the last occurrence of a substring in the string.
871
+
- `slice()`: Returns a section or the index to the beginning of the specified portion of stringObj
872
+
- `starsWith()`: Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.
873
+
- `endsWith()`: Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at endPosition – length(this). Otherwise returns false.
874
+
- `padStart`: Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the start (left) of the current string.
875
+
- `padEnd`: Pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.
0 commit comments