Ruby 执行string.slice()后,返回已删除部分的字符串

Ruby 执行string.slice()后,返回已删除部分的字符串,ruby,string,slice,Ruby,String,Slice,我相信这是很明显的,但我似乎无法让它工作。 我有一个Pathname实例,我试图从中删除第一个目录,然后返回字符串的其余部分,但由于slice返回已删除的部分,因此似乎无法恢复较小的字符串 filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html") filedir = filepath.to_s.slice("this_folder_needs_to_go/") newfilepath = filedir

我相信这是很明显的,但我似乎无法让它工作。 我有一个Pathname实例,我试图从中删除第一个目录,然后返回字符串的其余部分,但由于slice返回已删除的部分,因此似乎无法恢复较小的字符串

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filedir = filepath.to_s.slice("this_folder_needs_to_go/")
newfilepath = filedir
我希望
newfilepath
将是
另一个文件夹/file.html
,但它只是返回
这个文件夹\u需要\u去/

那么,我到底该如何获得已移除部分的字符串呢?

使用:

使用:

使用:

使用:


你也可以使用。切片!如果您试图修改字符串,则替换.slice

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filepath.to_s.slice!("this_folder_needs_to_go/")
puts filepath ==> "another_folder/file.html"

尽管这将不可逆地影响filepath变量。

您也可以使用.slice!如果您试图修改字符串,则替换.slice

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filepath.to_s.slice!("this_folder_needs_to_go/")
puts filepath ==> "another_folder/file.html"

尽管这将不可逆地影响filepath变量。

您也可以使用.slice!如果您试图修改字符串,则替换.slice

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filepath.to_s.slice!("this_folder_needs_to_go/")
puts filepath ==> "another_folder/file.html"

尽管这将不可逆地影响filepath变量。

您也可以使用.slice!如果您试图修改字符串,则替换.slice

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
filepath.to_s.slice!("this_folder_needs_to_go/")
puts filepath ==> "another_folder/file.html"
尽管这将不可逆地影响您的filepath变量。

分区
也将以与
拆分
类似的方式工作

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
file_dir = filepath.partition("this_folder_needs_to_go/")[-1]
file_dir
#=> "another_folder/file.html"
#partition
返回[head,sep,tail],因此这看起来像
[,“这个文件夹需要去”,“另一个文件夹/file.html”]
这个
[-1]
表示获取最后一个元素。

#partition
的工作方式与
#split
类似

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
file_dir = filepath.partition("this_folder_needs_to_go/")[-1]
file_dir
#=> "another_folder/file.html"
#partition
返回[head,sep,tail],因此这看起来像
[,“这个文件夹需要去”,“另一个文件夹/file.html”]
这个
[-1]
表示获取最后一个元素。

#partition
的工作方式与
#split
类似

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
file_dir = filepath.partition("this_folder_needs_to_go/")[-1]
file_dir
#=> "another_folder/file.html"
#partition
返回[head,sep,tail],因此这看起来像
[,“这个文件夹需要去”,“另一个文件夹/file.html”]
这个
[-1]
表示获取最后一个元素。

#partition
的工作方式与
#split
类似

filepath = Pathname.new("this_folder_needs_to_go/another_folder/file.html")
file_dir = filepath.partition("this_folder_needs_to_go/")[-1]
file_dir
#=> "another_folder/file.html"

#partition
返回[head,sep,tail],所以这看起来像
[,“这个文件夹需要走”,“/other\u folder/file.html”]
这个
[-1]
表示获取最后一个元素。

如果路径每次长度都不同,那也行吗?@SamMason,是的,上面的表达式通过
/
将字符串拆分为两部分,然后获得第二部分。如果路径每次长度都不同,那会怎么样?@SamMason,是的,上面的表达式通过
/
将字符串拆分为两部分,然后得到第二部分。如果路径每次长度都不同,那也行吗?@SamMason,是的,上面的表达式通过
/
将字符串分成两部分,然后得到第二部分。如果路径每次长度都不同,那也行吗?@SamMason,是的,上面的表达式通过
/
将字符串拆分为两部分,并获得第二部分。