0

C# - String.Remove Ex.

int j = 4;
string pages = "/.././ewcwe/";
pages = pages.Remove(j, 2);
// pages = "/../ewcwe/";
// delete ./

Is there such a function in python string.remove?

sorry, **./** -> ./

  • similar issue (somehow): http://stackoverflow.com/questions/6317500/how-can-i-splice-a-string – JMax Jun 06 '12 at 07:13
  • actually, you can probably find your answer here: http://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python – JMax Jun 06 '12 at 07:14
  • 3
    What does the `2` mean? You seem to be removing 6 characters here – John La Rooy Jun 06 '12 at 07:20

4 Answers4

3

Strings are immutable, so you need to extract the parts you want and create a new string from them:

s = pages[:4] + pages[10:]

Alternatively you can overwrite the existing string (as pointed out in the comment below):

pages = pages[:4] + pages[10:]
Pep_8_Guardiola
  • 5,002
  • 1
  • 24
  • 35
  • 2
    I don't know C# at all, but the example is `pages = pages.Remove(4, 2)`, in which `pages.Remove` appears to be returning its result rather than modifying `pages` as a side effect. So the immutability of strings appears to be irrelevant. – Ben Jun 06 '12 at 07:20
1

You can write your own metod.

>>>s="vivek"
>>> def rep(s,st,ed):
...  return s[:st]+s[ed:]
...
>>> rep(s,2,3)
'viv'
Vivek S
  • 5,384
  • 8
  • 51
  • 72
0

No strings don't have such a method

However if you use a bytearray it's easy to remove a slice

pages = bytearray(pages)
del pages[4:10]

Possibly what you are trying to achieve could be done better with a regular expression

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
-2

Simply No

need to use Regular Expressions for faster performance

dilip kumbham
  • 703
  • 6
  • 15