Need to compare strings programmatically? This guide covers everything you need to know about string comparison via API, including different comparison modes and handling special cases.
What is String Comparison?
String comparison determines the relationship between two strings - whether they're equal, which comes first alphabetically, or how similar they are. This is fundamental for sorting, searching, and validating text data.
Example: Comparing "Apple" and "apple" might return equal (case-insensitive) or not equal (case-sensitive).
Types of Comparison
Different comparison approaches:
- Exact match: Strings must be identical
- Case-insensitive: Ignore letter case differences
- Lexicographic: Alphabetical ordering comparison
- Similarity: Percentage of matching content
Using the String Compare API
TinyFn provides a simple endpoint to compare strings:
POST https://api.tinyfn.io/v1/text/compare
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"string1": "Hello World",
"string2": "hello world",
"case_sensitive": false
}
{
"equal": true,
"comparison": 0,
"case_sensitive": false,
"length_diff": 0
}
Parameters
| Parameter | Type | Description |
|---|---|---|
string1 |
string | First string to compare (required) |
string2 |
string | Second string to compare (required) |
case_sensitive |
boolean | Case-sensitive comparison (default: true) |
trim |
boolean | Trim whitespace before comparing (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/compare',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
string1: 'Hello World',
string2: 'hello world',
case_sensitive: false
})
}
);
const result = await response.json();
console.log(result.equal); // true
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/compare',
headers={'X-API-Key': 'your-api-key'},
json={'string1': 'Hello', 'string2': 'hello', 'case_sensitive': False}
)
result = response.json()
print(result['equal']) # True
cURL
curl -X POST "https://api.tinyfn.io/v1/text/compare" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"string1": "Hello", "string2": "hello", "case_sensitive": false}'
Common Use Cases
- Form Validation: Compare password confirmation fields
- Sorting: Determine alphabetical order
- Deduplication: Find identical or similar entries
- Search: Match user queries to content
- Testing: Compare expected vs actual outputs
Best Practices
- Normalize input: Consider trimming and case normalization
- Handle Unicode: Ensure proper handling of accented characters
- Choose comparison type: Match the type to your use case
- Consider locale: Sorting rules vary by language
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-text": {
"url": "https://api.tinyfn.io/mcp/text/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all text analysis tools available via MCP in our Text Analysis MCP Tools for AI Agents guide.
Try the String Compare API
Get your free API key and start comparing strings in seconds.
Get Free API Key