Find in string

3 snippets across 3 stacks - Bash & Linux, JavaScript, SQL

Also written as search string

SHBash & Linux

Search Text with Patterns

SH · Text Processing
Syntax
grep [options] pattern [file...]
Example
grep -rn 'TODO' src/
grep -i 'error' /var/log/syslog
grep -c 'SELECT' queries.sql
grep -v '^#' nginx.conf
Output
src/api/handler.go:42:  // TODO: add rate limiting
src/lib/cache.ts:18:  // TODO: implement TTL

Note -r searches recursively through directories. -n shows line numbers. -i is case-insensitive. -v inverts the match (shows non-matching lines). -c counts matches. -l lists only filenames with matches. Use -E for extended regex or egrep.

JSJavaScript

String Searching

JS · Strings
Syntax
str.includes(search, startIndex)
str.startsWith(search)
str.endsWith(search)
Example
const msg = "Order #1234 shipped";
console.log(msg.includes("shipped"));   // true
console.log(msg.startsWith("Order"));   // true
console.log(msg.endsWith("shipped"));   // true
console.log(msg.indexOf("#"));          // 6

Note All search methods are case-sensitive. For case-insensitive checks, lowercase both sides first: str.toLowerCase().includes(term.toLowerCase()).

SQLSQL

POSITION / STRPOS

SQL · String Functions
Syntax
POSITION(substring IN string)
STRPOS(string, substring)  -- PostgreSQL
Example
SELECT
  email,
  POSITION('@' IN email) AS at_position,
  SUBSTRING(email FROM POSITION('@' IN email) + 1) AS domain
FROM users;
Output
-- email             | at_position | domain
-- [email protected] | 6           | example.com

Note Returns the 1-based position of the first occurrence. Returns 0 if not found (not -1 like most programming languages). MySQL uses LOCATE(substring, string) which has the arguments in reverse order.

Frequently asked questions

How do you find in string?
This task is covered in 3 stacks on this page: Bash & Linux, JavaScript, SQL. The "Search Text with Patterns" snippet in Bash & Linux uses `grep [options] pattern [file...]`.
Which command does the Bash & Linux example use?
The "Search Text with Patterns" snippet uses `grep [options] pattern [file...]`, from the Text Processing section of the Bash & Linux cheat sheet.
Which stacks cover "find in string" on this page?
Bash & Linux, JavaScript, SQL. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Search Text with Patterns": -r searches recursively through directories. -n shows line numbers. -i is case-insensitive. -v inverts the match (shows non-matching lines). -c counts matches. -l lists only filenames with matches. Use -E for extended regex or egrep.