Normally, you would just do:

s = s[:-3] + s[-2:]

The s[:-3] gives you a string up to, but not including, the comma you want removed ("this is a string") and the s[-2:] gives you another string starting one character beyond that comma (" a").

Then, joining the two strings together gives you what you were after ("this is a string a").

Answer from Anurag Uniyal on Stack Overflow
🌐
ProPrep
proprep.com › questions › what-is-a-string-slice-how-is-it-useful
What is a string slice? How is it useful?
Stuck on a STEM question? Post your question and get video answers from professional experts: A string slice in programming refers to a subset or segment of ...
🌐
SamanthaMing
samanthaming.com › basics › string-slice
String - slice() | SamanthaMing.com
const name = 'samantha'; name.slice(0, 3); const string = 'Web Basics'; string.slice(0); // Clone the string // 'Web Basics' string.slice(); // Default is 0, so it will just clone // 'Web Basics' string.slice(-1); // Get the last letter // 's' Let's see some examples with an end argument.
Discussions

python - Ways to slice a string? - Stack Overflow
I have a string, example: s = "this is a string, a" Where a ',' (comma) will always be the 3rd to the last character, aka s[-3]. I am thinking of ways to remove the ',' but can only think of conv... More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between String.slice and String.substring?
If either argument is greater than the string's length, the string's length will be used instead. ... If start > stop, then substring will swap those 2 arguments. If either argument is negative or is NaN, it is treated as if it were 0. ... If start > stop, slice() will return the empty string. More on stackoverflow.com
🌐 stackoverflow.com
Return slice of struct of string?
Its a bit hard to deduce what the cause is without the lexer code, or a full minimal example. If I do something like this: pub fn lexer(alloc: std.mem.Allocator) ![]Token { var list = std.ArrayList(Token).init(alloc); var x: usize = 0; while(x < 4) : (x += 1) { var token = .{ .label = @intToEnum(Label, x), .value = std.ArrayList(u8).init(alloc) }; try token.value.append(42); try token.value.append(42); try token.value.append(42); try list.append(token); } return list.toOwnedSlice(); } test "lexer" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var result = try lexer(arena.allocator()); try std.testing.expect(result.len == 4); try std.testing.expect(result[0].label == .a); try std.testing.expect(result[0].value.items.len == 3); try std.testing.expect(result[0].value.items[0] == 42); var slice = result[0].value.toOwnedSlice(); // data moved from value.items to the slice try std.testing.expect(result[0].value.items.len == 0); try std.testing.expect(slice.len == 3); try std.testing.expect(slice[0] == 42); } Do note that once you call "toOwnedSlice()" you cannot use "token.value" again to get the data, since you now own that piece of data in the slice, and the ArrayList is emptied. If you only need the slice for some operation and want to keep the data then you can just do value.items to get the current slice of items without "owning" it. More on reddit.com
🌐 r/Zig
6
3
August 2, 2022
How to keep both a String and it's slice in a struct?

This is a self-referential struct, and it isn't very well supported by rust. Your From implementation only works because you're making a copy of the &'a str you're passing in, but that doesn't seem to be what you want by the title.

What are you trying to do? Maybe you can get the indices from split and save those as an alternative.

More on reddit.com
🌐 r/rust
22
8
June 28, 2017
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › slice
String.prototype.slice() - JavaScript | MDN
The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string.
🌐
Cycling '74
docs.cycling74.com › reference › string.slice
string.slice - Max Reference | Cycling '74 Documentation
string.slice is similar to string.substring, but attempts to conform to the JavaScript String.slice() behavior.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
1 week ago - Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.
Find elsewhere
🌐
Real Python
realpython.com › lessons › string-slicing
String Slicing (Video) – Real Python
In the previous lesson, you saw how you could access individual characters in a string using indexing. In this lesson, you’ll learn how to expand that syntax to extract substrings from a string. This technique is known as string slicing. You’ll practice with the standard syntax, and learn how omitting the first or last index extends the slice.
Published   October 1, 2019
🌐
The Rust Programming Language
doc.rust-lang.org › book › ch04-03-slices.html
The Slice Type - The Rust Programming Language
Slices let you reference a contiguous sequence of elements in a collection. A slice is a kind of reference, so it does not have ownership. Here’s a small programming problem: Write a function that takes a string of words separated by spaces and returns the first word it finds in that string.
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-slicing-in-python
String Slicing in Python - GeeksforGeeks
String slicing in Python is a way to get specific parts of a string by using start, end and step values. It’s especially useful for text manipulation and data parsing. ... Explanation: In this example, we used the slice s[0:5] to obtain the ...
Published   July 12, 2025
🌐
Electricimp
developer.electricimp.com › squirrel › string › slice
string.slice() | Dev Center
This method copies a sequence of characters from the target string into a new string which it returns. It takes as its first argument the index of the first character to be copied from the source string.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › slice
JavaScript String slice() - Extract Substring | Vultr Docs
November 15, 2024 - It allows you to extract a substring from a string based on specific start and end points, making it highly useful in text processing, data manipulation, and frontend development scenarios.
🌐
W3Schools
w3schools.com › python › gloss_python_string_slice.asp
Python Slice Strings
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... You can return a range of characters by using the slice syntax.
🌐
Attio
attio.com › engineering › blog › javascript-string-slice-considered-harmful
JavaScript string slice() considered harmful | Attio
Looking at the output of truncateSortableValue for my CSV file, I found an odd value that involved a flag character. When .slice() landed in the middle of a flag emoji, you sometimes ended up with unexpected results.
Top answer
1 of 9
1234

slice() works like substring() with a few different behaviors.

Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);

What they have in common:

  1. If start equals stop: returns an empty string
  2. If stop is omitted: extracts characters to the end of the string
  3. If either argument is greater than the string's length, the string's length will be used instead.

Distinctions of substring():

  1. If start > stop, then substring will swap those 2 arguments.
  2. If either argument is negative or is NaN, it is treated as if it were 0.

Distinctions of slice():

  1. If start > stop, slice() will return the empty string. ("")
  2. If start is negative: sets char from the end of string, exactly like substr().
  3. If stop is negative: sets stop to: string.length – Math.abs(stop) (original value), except bounded at 0 (thus, Math.max(0, string.length + stop)) as covered in the ECMA specification.

Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()

2 of 9
190

TL;DR:

  • If you know the index (the position) on which you'll stop (but NOT include), use slice().
  • If you know the length of characters to be extracted, you could use substr(), but that is discouraged as it is deprecated.

Otherwise, read on for a full comparison

Syntax

  • string.slice(start,end)
  • string.substr(start,length)
  • string.substring(start,end)

Note #1: slice()==substring()

What it does?

  • slice() extracts parts of a string and returns the extracted parts in a new string.
  • substr() extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
  • substring() extracts parts of a string and returns the extracted parts in a new string.

Note #2: slice()==substring()

Changes the Original String?

  • slice() doesn't
  • substr() doesn't
  • substring() doesn't

Note #3: slice()==substr()==substring()

Using Negative Numbers as an Argument

  • slice() selects characters starting from the end of the string
  • substr() selects characters starting from the end of the string
  • substring() doesn't perform

Note #4: slice()==substr()

If the First Argument is Greater than the Second

  • slice() doesn't perform
  • substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
  • substring() will swap the two arguments, and perform as usual

The First Argument

  • slice() required; starting Index
  • substr() required; starting Index
  • substring() required; starting Index

Note #5: slice()==substr()==substring()

The Second Argument

  • slice() optional; the position (up to, but not including) where to end the extraction
  • substr() optional; the number of characters to extract
  • substring() optional; the position (up to, but not including) where to end the extraction

Note #6: slice()==substring()

What if the Second Argument is Omitted?

  • slice() selects all characters from the start-position to the end of the string
  • substr() selects all characters from the start-position to the end of the string
  • substring() selects all characters from the start-position to the end of the string

Note #7: slice()==substr()==substring()

So, you can say that there's a difference between slice() and substr(), while substring() is basically a copy of slice().

If you want substr's functionality:

"foobarbaz".substr(index, length);

without using a deprecated feature, you can just do:

"foobarbaz".substring(index, length + index);

And get the exact same results bar all of the edge-cases, like negative index/length.

🌐
Mimo
mimo.org › glossary › javascript › string-slice
JavaScript String slice() Method: Syntax, Usage, and Examples
The JavaScript slice() method is commonly associated with arrays, but it's also a powerful method available on strings. The JavaScript string slice method allows you to extract a portion of a string and return it as a new string, leaving the original string untouched.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.slice.html
pandas.Series.str.slice — pandas 3.0.1 documentation
Slice substrings from each element in the Series or Index. Slicing substrings from strings in a Series or Index helps extract specific portions of data, making it easier to analyze or manipulate text.
🌐
Go by Example
gobyexample.com › slices
Go by Example: Slices
Slices are an important data type in Go, giving a more powerful interface to sequences than arrays · Unlike arrays, slices are typed only by the elements they contain (not the number of elements). An uninitialized slice equals to nil and has length 0
🌐
Online String Tools
onlinestringtools.com › slice-string
Slice a String – Online String Tools
Simple, free and easy to use online tool that slices string. No intrusive ads, popups or nonsense, just a string slicer. Load a string, slice string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-slice-method
JavaScript String slice() Method - GeeksforGeeks
July 11, 2025 - The slice() method in JavaScript is used to extract a portion of a string and create a new string without modifying the original string.