0
# String Processing
1
2
String manipulation utilities including random generation, splitting with trimming, HTTP header validation, and character replacement operations optimized for web applications and data processing.
3
4
## Capabilities
5
6
### Random String Generation
7
8
Generate random strings with customizable length and character sets.
9
10
```typescript { .api }
11
/**
12
* Generate random string with specified length and character set
13
* @param length - String length (defaults to 16)
14
* @param charSet - Character set to use (defaults to alphanumeric)
15
* @returns Random string
16
*/
17
function randomString(length?: number, charSet?: string): string;
18
```
19
20
**Usage Examples:**
21
22
```typescript
23
import { randomString } from "utility";
24
25
// Default 16-character alphanumeric string (A-Z, a-z, 0-9)
26
const id = randomString();
27
// Result: "a8B3kN9mP2xR7wQ5"
28
29
// Custom length
30
const token = randomString(32);
31
// Result: 32-character random string
32
33
// Numbers only (verification codes)
34
const numCode = randomString(6, '0123456789');
35
// Result: "482759"
36
37
// Hexadecimal characters (color codes, IDs)
38
const hexId = randomString(8, '0123456789ABCDEF');
39
// Result: "A7B2C8F3"
40
41
// Lowercase letters only (readable IDs)
42
const readableId = randomString(8, 'abcdefghijklmnopqrstuvwxyz');
43
// Result: "xkjmplvr"
44
45
// Uppercase letters only (codes)
46
const upperCode = randomString(4, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
47
// Result: "QMXP"
48
49
// URL-safe characters (tokens for URLs)
50
const urlSafe = randomString(16, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
51
// Result: "A7b-X9_m2PqR8wK5"
52
53
// Custom special set (PIN codes avoiding confusing characters)
54
const pin = randomString(4, '23456789ABCDEFGHJKLMNPQRSTUVWXYZ');
55
// Result: "7H4K" (excludes 0, 1, I, O to avoid confusion)
56
57
// Binary string
58
const binary = randomString(8, '01');
59
// Result: "10110100"
60
61
// DNA sequence simulation
62
const dna = randomString(12, 'ATCG');
63
// Result: "ATCGATCGGTCA"
64
65
// Password-like with special characters
66
const password = randomString(12, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*');
67
// Result: "K8m@P2x#R7w!"
68
```
69
70
### String Splitting with Trimming
71
72
Split strings with automatic trimming and empty element removal.
73
74
```typescript { .api }
75
/**
76
* Split string to array with trimming and empty removal
77
* @param str - Input string (defaults to empty string)
78
* @param sep - Separator (defaults to comma)
79
* @returns Array of trimmed, non-empty strings
80
*/
81
function split(str?: string, sep?: string): string[];
82
83
// Alias for compatibility
84
const splitAlwaysOptimized = split;
85
```
86
87
**Usage Examples:**
88
89
```typescript
90
import { split, splitAlwaysOptimized } from "utility";
91
92
// Default comma separation with trimming
93
const tags = split(" tag1 , tag2, tag3 ");
94
// Result: ["tag1", "tag2", "tag3"]
95
96
// Custom separator
97
const paths = split("/path/to/file", "/");
98
// Result: ["path", "to", "file"]
99
100
// Handles empty elements
101
const messy = split("a,,b, ,c", ",");
102
// Result: ["a", "b", "c"]
103
104
// Using alias
105
const cleaned = splitAlwaysOptimized(" item1 ; item2 ; ", ";");
106
// Result: ["item1", "item2"]
107
```
108
109
### String Replacement
110
111
Advanced string replacement with function support.
112
113
```typescript { .api }
114
/**
115
* Replace string with support for function replacers
116
* @param str - Input string
117
* @param substr - String or RegExp to find
118
* @param newSubstr - Replacement string or function
119
* @returns Modified string
120
*/
121
function replace(str: string, substr: string | RegExp, newSubstr: string | StringReplacer): string;
122
123
type StringReplacer = (substring: string, ...args: any[]) => string;
124
```
125
126
**Usage Examples:**
127
128
```typescript
129
import { replace } from "utility";
130
131
// Simple string replacement
132
const basic = replace("hello world", "world", "universe");
133
// Result: "hello universe"
134
135
// Function-based replacement
136
const dynamic = replace("hello WORLD", /[A-Z]+/g, (match) => match.toLowerCase());
137
// Result: "hello world"
138
139
// Complex replacement with capture groups
140
const formatted = replace("2025-09-06", /(\d{4})-(\d{2})-(\d{2})/, (match, year, month, day) => {
141
return `${day}/${month}/${year}`;
142
});
143
// Result: "06/09/2025"
144
```
145
146
### HTTP Header Validation
147
148
Validate and clean HTTP header values according to RFC specifications.
149
150
```typescript { .api }
151
/**
152
* Replace invalid HTTP header characters with replacement
153
* @param val - Input header value
154
* @param replacement - Replacement string or function
155
* @returns Object with cleaned value and validity flag
156
*/
157
function replaceInvalidHttpHeaderChar(val: string, replacement?: string | Replacement): {val: string, invalid: boolean};
158
159
/**
160
* Detect invalid HTTP header characters in a string
161
* @param val - Input string to validate
162
* @returns True if string contains invalid characters
163
*/
164
function includesInvalidHttpHeaderChar(val: string): boolean;
165
166
type Replacement = (char: string) => string;
167
```
168
169
**Usage Examples:**
170
171
```typescript
172
import { replaceInvalidHttpHeaderChar, includesInvalidHttpHeaderChar } from "utility";
173
174
// Check for invalid characters
175
const hasInvalid = includesInvalidHttpHeaderChar("Valid-Header-Value");
176
// Result: false
177
178
const hasInvalidChars = includesInvalidHttpHeaderChar("Invalid\nHeader");
179
// Result: true
180
181
// Clean invalid characters
182
const cleaned = replaceInvalidHttpHeaderChar("Header\nWith\rNewlines");
183
// Result: { val: "Header With Newlines", invalid: true }
184
185
// Custom replacement
186
const customClean = replaceInvalidHttpHeaderChar("Bad\x00Chars", "_");
187
// Result: { val: "Bad_Chars", invalid: true }
188
189
// Function-based replacement
190
const funcClean = replaceInvalidHttpHeaderChar("Test\nValue", (char) => {
191
return char === '\n' ? ' ' : '_';
192
});
193
// Result: { val: "Test Value", invalid: true }
194
195
// No invalid characters
196
const alreadyClean = replaceInvalidHttpHeaderChar("Clean-Header");
197
// Result: { val: "Clean-Header", invalid: false }
198
```