String Functions in MATLAB
🔤 String Functions in MATLAB
Modern MATLAB encourages using string arrays ("text") instead of character arrays ('text').
MATLAB is developed by MathWorks.
🔹 String vs Character Array (Quick)
| Type | Example | Notes |
|---|---|---|
| String | "Hello" |
Recommended (rich functions, array-friendly) |
| Char | 'Hello' |
Legacy; still supported |
1️⃣ Creating Strings
Create from char:
2️⃣ Length & Size
Output
3️⃣ Concatenation
🔸 + operator (recommended)
Output
🔸 strcat()
4️⃣ Change Case
Output
5️⃣ Comparison
Output
6️⃣ Search & Match
🔸 contains()
Output
🔸 startsWith() / endsWith()
7️⃣ Replace Text
Output
8️⃣ Split & Join
🔸 split()
Output
🔸 join()
Output
9️⃣ Trim Spaces
Output
🔟 Convert Between Types
Output
1️⃣1️⃣ Formatting Strings
Output
1️⃣2️⃣ String Arrays
Output
⚠️ Important Notes
-
Prefer string arrays (
" ") over char arrays -
Many functions are vectorized for string arrays
-
Use
sprintffor formatted messages -
contains,replace,splitare exam favorites
🎯 Interview Questions: String Functions in MATLAB
🔹 Q1. Difference between string and char?
Answer:
Strings use double quotes and support richer functions; chars use single quotes.
🔹 Q2. How do you concatenate strings?
Answer:
Using + or strcat().
🔹 Q3. Which function checks substring existence?
Answer:contains().
🔹 Q4. How do you split a string?
Answer:
Using split().
🔹 Q5. How do you format strings with values?
Answer:
Using sprintf().
🔹 Q6. How do you convert string to number?
Answer:str2double().
✅ Summary
-
String functions handle text efficiently
-
Use
" "(string) instead of' '(char) -
Support searching, replacing, splitting, formatting
-
Essential for file I/O, labels, and reports
