strstr returns a pointer to the found character, so you could use pointer arithmetic: (Note: this code not tested for its ability to compile, it's one step away from pseudocode.)
Copychar * source = "test string"; /* assume source address is */
/* 0x10 for example */
char * found = strstr( source, "in" ); /* should return 0x18 */
if (found != NULL) /* strstr returns NULL if item not found */
{
int index = found - source; /* index is 8 */
/* source[8] gets you "i" */
}
Answer from Bill on Stack OverflowString.indexOf function in C - Stack Overflow
c# - What does string.IndexOf do? - Stack Overflow
How does the `indexOf` method on a string work in JavaScript? - Stack Overflow
Why can Indexof find string but not substring?
Videos
strstr returns a pointer to the found character, so you could use pointer arithmetic: (Note: this code not tested for its ability to compile, it's one step away from pseudocode.)
Copychar * source = "test string"; /* assume source address is */
/* 0x10 for example */
char * found = strstr( source, "in" ); /* should return 0x18 */
if (found != NULL) /* strstr returns NULL if item not found */
{
int index = found - source; /* index is 8 */
/* source[8] gets you "i" */
}
I think that
size_t strcspn ( const char * str1, const char * str2 );
is what you want. Here is an example pulled from here:
Copy/* strcspn example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "fcba73";
char keys[] = "1234567890";
int i;
i = strcspn (str,keys);
printf ("The first number in str is at position %d.\n",i+1);
return 0;
}
Returns the first appearance of a specified char. For example
string x = "Hello World";
x.indexOf("W");
it will return 6 (0 based count).
The overloads let you choose for example, where you want to start searching.. like
x.indexOf("W", 7); it will return -1 because W is at position 6 so if starts at 7 it won't find any.
I hope this helps ! the best way is to play with it
This also works with arrays.
I believe the MSDN explanation is pretty clear.
For example:
string something = "something";
int indexOfT = something.IndexOf("t"); // => returns 4
Reports the zero-based index of the first occurrence of the specified string in this instance.
So if "t" is in the fifth position of "something", 4 would be it's zero-based index.
The indexOf function of a string does not return a character but rather returns the position of the first place that string or character is found, not the character itself.
In this question, the last character is "p", as found by this line:
var lastChar = sentence[sentence.length - 1];
However when looking at the string, you can see that the letter "p" is found more than once:
"welcome to bootcamp prep"
// ^ ^ ^
// 18 20 23
The numbers indicated in the comment (18, 20, and 23) represent the indexes (locations) of the character within the string.
What do these indexes mean? In JavaScript, indexes start at 0, so the first letter ("w") will be at index 0. In this string, the character "p" is found at places 19, 21, and 24, which translate to the indexes 18, 20, and 23.
The first instance of the searched string "p" is at index 18. Therefore, when running this line:
console.log(sentence.indexOf(lastChar)); // 18
The result will be 18, the index of the first instance of the last character.
(Side note: assuming you are a beginner when it comes to JavaScript - try not to use the var keyword whenever possible. If the variable needs to change, use let, or if the variable stays the same, use const.)
Because indexOf returns the first letter that is found matching the argument.
welcome to bootcam[p] <-- here prep is found.
JavaScript uses primitive data types so you string is just a string and holds no reference to the original value.