Finding the position of words in a string
To get the "ordinal" positions of search string jake
in the input string use the following approach:
mystr = "there not what is jake can do for you ask what you play do for spare jake"
search_str = 'jake'
result = [i+1 for i,w in enumerate(mystr.split()) if w.lower() == search_str]
print(result)
The output:
[5, 17]
enumerate(mystr.split())
- to get enumerated object (pairs of items with their positions/indices)w.lower() == search_str
- if a word is equal to search string
Find position of word in string
You can use find_in_set()
for this:
select find_in_set('lovely', replace('good morning what a lovely day today', ' ', ','))
Yields:
5
From the documentation:
FIND_IN_SET(str,strlist)
Returns a value in the range of
1
toN
if the stringstr
is in the string liststrlist
consisting ofN
substrings. A string list is a string composed of substrings separated by,
characters.
So basically the logic is to replace space characters with commas, and then use that function.
Related Topics
How to Reload Google Chrome Tab from Terminal
How to Send a File as an Email Attachment Using Linux Command Line
Best Practices When Running Node.Js With Port 80 (Ubuntu/Linode)
How to Debug the Linux Kernel With Gdb and Qemu
Pipe To/From the Clipboard in a Bash Script
Find Multiple Files and Rename Them in Linux
Which Linux Ipc Technique to Use
How to Find Out Which Processes Are Using Swap Space in Linux
Does Malloc Lazily Create the Backing Pages For an Allocation on Linux (And Other Platforms)
Compiling C++ on Remote Linux Machine - "Clock Skew Detected" Warning
How to Simulate Just One Enter in Command Line After Executing a Jar File
How to Fix 'Sudo: No Tty Present and No Askpass Program Specified' Error
Confused About Stdin, Stdout and Stderr
How to Print a Number in Assembly Nasm
Performing Http Requests With Curl (Using Proxy)