2.3 Text Operations Across Tools
ทุกเครื่องมือมี text operations พื้นฐานเหมือนกัน แต่เรียกชื่อต่างกัน — ถ้าคุณรู้ “concept” คุณจะหาคำสั่งที่ถูกต้องได้ในทุกเครื่องมือ
6 Operations ที่ใช้บ่อยที่สุด
Section titled “6 Operations ที่ใช้บ่อยที่สุด”เราจะดู 6 operations นี้ในทุกเครื่องมือ:
- Length — นับความยาวของ text
- First N chars — ตัดเอาตัวอักษรแรก N ตัว
- Find — ค้นหาตำแหน่งของ substring
- Strip / Trim — ตัด spaces หน้า-หลัง
- Pad — เติมอักขระนำหน้า (เช่น เติม 0)
- Concatenate — ต่อ text เข้าด้วยกัน
ตารางเปรียบเทียบ
Section titled “ตารางเปรียบเทียบ”| Operation | Google Sheets | Python (str) | pandas (Series.str) | SQL | C |
|---|---|---|---|---|---|
| Length | =LEN(A1) | len(s) | df['col'].str.len() | LENGTH(col) | strlen(s) |
| First 3 | =LEFT(A1,3) | s[:3] | df['col'].str[:3] | LEFT(col,3) / SUBSTRING(col,1,3) | strncpy(dst,s,3) |
| Last 3 | =RIGHT(A1,3) | s[-3:] | df['col'].str[-3:] | RIGHT(col,3) | s+strlen(s)-3 |
| Find | =FIND("x",A1) | s.find("x") | df['col'].str.find("x") | POSITION('x' IN col) | strstr(s,"x") |
| Trim | =TRIM(A1) | s.strip() | df['col'].str.strip() | TRIM(col) | ต้องเขียนเอง |
| Pad left | =REPT("0",5-LEN(A1))&A1 | s.zfill(5) | df['col'].str.zfill(5) | LPAD(col,5,'0') | sprintf(buf,"%05s",s) |
| Concat | =A1&B1 / =CONCAT(A1,B1) | a + b / f"{a}{b}" | df['a'] + df['b'] | col1 || col2 / CONCAT(col1,col2) | strcat(dst,src) |
ใส่ Ploy Srisuwan (มี spaces หน้า-หลัง) ในเซลล์ A1 แล้วลอง:
=LEN(A1) → 18 (รวม spaces)=LEN(TRIM(A1)) → 14 (ตัด spaces หน้า-หลัง)=LEFT(TRIM(A1),4) → "Ploy"=RIGHT(TRIM(A1),8) → "Srisuwan"=FIND("Sri",A1) → 8 (ตำแหน่งที่เจอ รวม leading spaces)Pad leading zeros:
ใส่ 123 ใน B1:
=REPT("0",5-LEN(B1))&B1 → "00123"=TEXT(B1,"00000") → "00123" (วิธีง่ายกว่า!)Concat:
ใส่ Ploy ใน C1 และ Srisuwan ใน D1:
=C1&" "&D1 → "Ploy Srisuwan"=CONCAT(C1," ",D1) → Error (CONCAT รับแค่ 2 arguments)=TEXTJOIN(" ",TRUE,C1,D1) → "Ploy Srisuwan" (ดีกว่า)#include <stdio.h>#include <string.h>
int main() { char name[] = "Ploy Srisuwan";
// Length printf("Length: %zu\n", strlen(name)); // 13
// First 4 chars char first[5]; strncpy(first, name, 4); first[4] = '\0'; printf("First 4: \"%s\"\n", first); // "Ploy"
// Find substring char *pos = strstr(name, "Sri"); if (pos != NULL) { printf("Find 'Sri': position %ld\n", pos - name); // 5 }
// Concatenate char full[100]; strcpy(full, "Hello "); strcat(full, name); printf("Concat: \"%s\"\n", full); // "Hello Ploy Srisuwan"
// Pad with zeros — ใช้ sprintf int id = 123; char padded[6]; sprintf(padded, "%05d", id); printf("Padded: \"%s\"\n", padded); // "00123"
// C ไม่มี trim ในตัว — ต้องเขียนเอง char messy[] = " Hello "; // ต้อง loop ตัด spaces ด้วยตัวเอง printf("Messy: \"%s\" (len=%zu)\n", messy, strlen(messy)); // " Hello " (len=9)
return 0;}Key insight: C ต้องจัดการหน่วยความจำเอง — strncpy ต้องใส่ null terminator เอง, strcat อาจ overflow ถ้า buffer เล็กเกินไป
import pandas as pd
name = " Ploy Srisuwan "
# Lengthprint(len(name)) # 18 (รวม spaces)print(len(name.strip())) # 13 (ตัด spaces)
# First 4 chars (หลัง strip)clean = name.strip()print(clean[:4]) # "Ploy"print(clean[-8:]) # "Srisuwan"
# Findprint(clean.find("Sri")) # 5 (0-indexed)print(clean.find("xyz")) # -1 (ไม่เจอ)
# Strip (3 แบบ)print(name.strip()) # "Ploy Srisuwan" (ตัดทั้ง 2 ข้าง)print(name.lstrip()) # "Ploy Srisuwan " (ตัดแค่ซ้าย)print(name.rstrip()) # " Ploy Srisuwan" (ตัดแค่ขวา)
# Padprint("123".zfill(5)) # "00123"print("123".rjust(5, '0')) # "00123"print("123".ljust(5, '0')) # "12300"
# Concatenatefirst = "Ploy"last = "Srisuwan"print(first + " " + last) # "Ploy Srisuwan"print(f"{first} {last}") # "Ploy Srisuwan" (f-string)
# pandas — ทำทั้ง column ได้ทีเดียวdf = pd.DataFrame({'name': [' Ploy ', 'Som', ' Tong '], 'id': ['123', '45', '6789']})
print(df['name'].str.strip()) # ตัด spaces ทั้ง columnprint(df['name'].str.strip().str[:3]) # ตัด 3 ตัวแรกprint(df['id'].str.zfill(5)) # 00123, 00045, 06789-- สมมติมี tableCREATE TABLE people ( id VARCHAR(10), name VARCHAR(100));
INSERT INTO people VALUES ('123', ' Ploy Srisuwan ');
-- LengthSELECT LENGTH(name), -- 18 (รวม spaces) LENGTH(TRIM(name)) -- 13FROM people;
-- First 4 / Last 8SELECT LEFT(TRIM(name), 4) AS first_4, -- Ploy RIGHT(TRIM(name), 8) AS last_8 -- SrisuwanFROM people;
-- SUBSTRING (ยืดหยุ่นกว่า LEFT/RIGHT)SELECT SUBSTRING(TRIM(name) FROM 1 FOR 4) AS first_4 -- PloyFROM people;
-- FindSELECT POSITION('Sri' IN name) AS pos -- 8 (1-indexed, รวม spaces)FROM people;
-- Trim (3 แบบ)SELECT TRIM(name), -- "Ploy Srisuwan" TRIM(LEADING FROM name), -- "Ploy Srisuwan " TRIM(TRAILING FROM name) -- " Ploy Srisuwan"FROM people;
-- PadSELECT LPAD(id, 5, '0') AS padded_id -- "00123"FROM people;
-- ConcatenateSELECT 'Hello ' || TRIM(name) AS greeting -- "Hello Ploy Srisuwan"FROM people;-- หรือSELECT CONCAT('Hello ', TRIM(name)) AS greetingFROM people;จุดต่างที่ต้องระวัง
Section titled “จุดต่างที่ต้องระวัง”| จุดต่าง | รายละเอียด |
|---|---|
| Index เริ่มที่ | Python/C: 0-indexed, SQL/Sheets: 1-indexed |
| TRIM behavior | Sheets TRIM ยุบ spaces ตรงกลางด้วย, SQL/Python ตัดแค่หน้า-หลัง |
| Find ไม่เจอ | Python: -1, Sheets: #VALUE!, SQL: 0, C: NULL pointer |
| LPAD ใน Sheets | ไม่มี LPAD — ต้องใช้ REPT()& หรือ TEXT() |