diff --git a/src/App.tsx b/src/App.tsx index 75bd97b..b22a470 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,6 +11,7 @@ import { Base64Page } from "@/pages/base64" import { GistPage } from "@/pages/gist" import { HomePage } from "@/pages/home" import { QRCodePage } from "@/pages/qrcode" +import { StringLengthPage } from "@/pages/string-length" import { TimestampPage } from "@/pages/timestamp" export function App() { @@ -30,6 +31,7 @@ export function App() { } /> } /> } /> + } /> diff --git a/src/components/app-sidebar.tsx b/src/components/app-sidebar.tsx index 62bfa2c..bffc0d4 100644 --- a/src/components/app-sidebar.tsx +++ b/src/components/app-sidebar.tsx @@ -32,6 +32,10 @@ const navItems = [ title: "Image", items: [{ title: "QR Code", url: "/qrcode" }], }, + { + title: "String", + items: [{ title: "String Length", url: "/string-length" }], + }, ] export function AppSidebar({ ...props }: React.ComponentProps) { diff --git a/src/pages/string-length.tsx b/src/pages/string-length.tsx new file mode 100644 index 0000000..b6b37e7 --- /dev/null +++ b/src/pages/string-length.tsx @@ -0,0 +1,65 @@ +import { useState } from "react" + +import { Textarea } from "@/components/ui/textarea" + +function countBytes(str: string): number { + return new TextEncoder().encode(str).length +} + +function countWords(str: string): number { + const trimmed = str.trim() + return trimmed === "" ? 0 : trimmed.split(/\s+/).length +} + +function countLines(str: string): number { + return str === "" ? 0 : str.split("\n").length +} + +export function StringLengthPage() { + const [input, setInput] = useState("") + + const charCount = input.length + const charNoSpaceCount = input.replace(/\s/g, "").length + const byteCount = countBytes(input) + const wordCount = countWords(input) + const lineCount = countLines(input) + + const stats: { label: string; value: number }[] = [ + { label: "Characters", value: charCount }, + { label: "Characters (no spaces)", value: charNoSpaceCount }, + { label: "Bytes (UTF-8)", value: byteCount }, + { label: "Words", value: wordCount }, + { label: "Lines", value: lineCount }, + ] + + return ( + + + String Length + + Count characters, bytes, words, and lines in a string. + + + + Input + setInput(e.target.value)} + placeholder="Type or paste text here..." + /> + + + {stats.map(({ label, value }) => ( + + {label} + {value} + + ))} + + + ) +}
+ Count characters, bytes, words, and lines in a string. +