SVG getSubStringLength() Method
getSubStringLength() measures the rendered width of a specific character range within SVG text, useful for text truncation and highlighting.
Drop your image here
Supports PNG, JPG, BMP, WEBP up to 5MB
Measuring a Portion of Text
Calling getSubStringLength(charnum, nchars) on an SVG text element returns the rendered width of the substring starting at the given character index and spanning the specified number of characters, letting you measure just a portion of a text element rather than its entire content.
- getSubStringLength(charnum, nchars) measures a specific character range
- Returns the rendered width of just that portion of the text
- Measures a portion rather than the entire text element's width
Practical Uses for Partial Text Measurement
This method is useful for building custom text truncation that measures character-by-character to find exactly where text should be cut to fit a width, for positioning a highlight or underline behind a specific word within a longer text run, or for implementing custom text selection or cursor positioning in an interactive SVG text editor.
- Builds custom truncation finding exactly where text should be cut to fit
- Positions a highlight or underline behind a specific word in a text run
- Supports custom text selection or cursor positioning in an SVG text editor
Frequently Asked Questions
How would I use getSubStringLength() to truncate text with an ellipsis?
You'd progressively measure substrings of increasing length until one exceeds your target width, then truncate just before that point and append an ellipsis, using the measured widths to find the exact cutoff character.
Does getSubStringLength() account for kerning between characters?
It measures the actual rendered advance of the specified substring including the effects of the applied font's spacing, giving a real rendered measurement of that character range.
Related guides
Ready to Convert Your Image to SVG?
Free online converter — no sign-up, no watermarks, results in under 3 seconds.
Try It Free — Convert Image to SVG