Extract a substring from a string value

Isolate a portion of a string identified by a range of characters.


Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):


Use this solution in your Compute@Edge service:

// s.chars().skip(a).take(b).collect()
// will take a substring from `s` across the range [a..(b+a))
// If [a..(b+a)) is out of range,
// we will take as much as possible without panicking.
let s = "afoobar";
let substr: String = s.chars().skip(1).take(3).collect();
// resp_body will be "left=foo"
let resp_body = format!("left={}", substr);

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.