Delete characters in a string Python

gharz
orig_string = "\\\\file_foo\\bar\\text-to-be-deleted\\foo-bar.pdf"

The original string needs to be modified (copy into a new variable) to look like the new_string below. The file has thousands of lines with the same format (file path of a pdf file).

new_string = "\\\\file_foo\\bar\\foo-bar.pdf"

How do I modify the orig_string to look like the new string?

Edit: Sorry, I forgot to mention on my original post. The '\text-to-be-deleted' is not the same. All filepath have different '\text-to-be-deleted' string.

Ex.

"\\\\file_foo\\bar\\path100\\foo-bar.pdf"
"\\\\file_foo\\bar\\path-sample\\foo-bar.pdf"
"\\\\file_foo\\bar\\another-text-be-deleted\\foo-bar.pdf"

... and so on.

Vimiix

I have a method. Hope it can help you.

orig_string = "\\\\file_foo\\bar\\text-to-be-deleted\\foo-bar.pdf"
back_index = orig_string.rfind('\\')
front_index = orig_string[:back_index].rfind('\\')
new_string = orig_string[:front_index] + orig_string[back_index:]
print(new_string)

Output

'\\\\file_foo\\bar\\foo-bar.pdf'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to delete specific characters from a string in Ruby?

From Dev

How to delete characters in a string according to a second string?

From Dev

Delete "very" special characters from string

From Dev

Delete the last two characters of the String

From Dev

Python string wrong characters

From Dev

How to delete characters in a string?

From Dev

Delete duplicate characters from string

From Dev

Sorting characters of a string in Python

From Dev

Match string with normal characters with special characters in python

From Dev

Delete characters in range of string

From Dev

Python: How to delete rows ending in certain characters?

From Dev

Algorithm to delete duplicate characters from a String

From Dev

Match a specific number of characters in string and delete python

From Dev

Python: Checking Characters in String

From Dev

Translating characters in a string to multiple characters using Python

From Dev

How to 'drop'/delete characters from in front of a string?

From Dev

Batch – Delete Characters in a String

From Dev

Trying to delete string characters

From Dev

Delete "very" special characters from string

From Dev

Python: place a characters in string

From Dev

Delete a certain number of characters after a string

From Dev

How to delete characters in a string?

From Dev

Delete duplicate characters from string

From Dev

Sorting characters of a string in Python

From Dev

reorder characters in a string in Python

From Dev

Delete characters in range of string

From Dev

sed command to delete a string containing / ,\ characters

From Dev

Separate string by comma and delete new line characters

From Dev

Delete specific characters in string C#

Related Related

HotTag

Archive