A startWith() function used to search string which starts with specified string. It returns boolean value either true or false. If string starts with specified string it returns true otherwise false.
1 <source_string>.startsWith(<search_string>, <starting_position>)
source_string : A source string in which we want to search for specified string at starting position
search_string : A search string to validate whether source string start with or not
starting_position : A starting position is optional, if specified, it will start searching string from the specified position.
1 let str = "hello world !";
2
3 str.startsWith("hello");
4
5 str.startsWith("world");
In the above example, we have define string with initial value. In the first check, it returns true as string start with word hello. In the second check, it will return false as it not starting with word world.
startsWith() function with position
1 let str = "hello world !!";
2
3 str.startsWith("hello", 5);
In the above example, we have define string with initial value and using startsWith() function validating source string starts with specified string from the given index. As it not starting with given search string, it will returns false.
startsWith() with string array
startsWith() with string array
1 function print(array, src) {
2 for(str of array) {
3 if(str.startsWith(src)) {
4 console.log(str);
5 }
6 }
7 }
8
9 let arr = [
10 "string function",
11 "string character",
12 "character at string"];
13
14
15 print(arr, "string");
In the above example, an array is define and initialized with string literals. A function define that accepts two parameters, an array and search string. An array is iterated using for...of loop and a if statement validates whether a string starts with specified search string using startsWith() function. If a function returns booean value true, a string will be print.
1 string function
2 string character
User define startsWith()
1 function startsWith(source, str, ind) {
2
3 let srt = !ind || ind < 0 ? 0 : ind;
4 let sLen = str.length;
5 let length = srt + sLen;
6
7 for(i = srt; i < length; i++) {
8 if(source[i] == str[0]) {
9 for(j = 0; j <= sLen; j++) {
10 if(source[i + j] != str[j]) {
11 return false;
12 }
13 if(j == sLen -1) {
14 return true;
15 }
16 }
17 } else {
18 break;
19 }
20 }
21 return false
22 }
23
24 let arr = [
25 "string function",
26 "string character",
27 "character at string"];
28
29 for(str of arr) {
30
31 if(startsWith(str, "string")) {
32 console.log(str);
33 }
34 }
In the above example, an array is define and initialized with string literals. A function is define that accepts three parameters. A source string, search string and starting index position. If string index less than 0 or not specified, it starts searching string from index position 0.
If specified index position characters match with search string, it validates next characters using nested for loop and if occurrence present, it returns boolean value true, otherwise false and If first character does not match, it return false.
1 string function
2 string character
Related options for your search