I have a string: "https://mhnkp2.com/src/vid/pra/Tentang_Mata-Cerita_Boneka-Prasekolah.mp4"
I want to get only "Tentang_Mata-Cerita_Boneka-Prasekolah"
I try to use code:
string path = "https:\/\/mhnkp2.com\/src\/vid\/pra\/Tentang_Mata-Cerita_Boneka-Prasekolah.mp4"
int start = path.IndexOf("/") + 5;
int end = path.IndexOf(".", start);
string result = path.Substring(start, end - start);
But the result is only "kp". How to handle it?
您可以使用
System.IO.Path.GetFileNameWithoutExtension("https://mhnkp2.com/src/vid/pra/Tentang_Mata-Cerita_Boneka-Prasekolah.mp4")
You could use one of these ways :
First, separate the string part :
var originalValue = @"https://mhnkp2.com/src/vid/pra/Tentang_Mata-Cerita_Boneka-Prasekolah.mp4";
var startPoint = originalValue.LastIndexOf('/') + 1 ;
var endPoint = originalValue.Length - startPoint;
var splitedValue = originalValue.Substring(startPoint, endPoint);
var dotIndex = splitedValue.LastIndexOf('.');
var extension = splitedValue.Substring(dotIndex, splitedValue.Length - dotIndex);
var finalValue = splitedValue.Replace(extension, string.Empty);
Second, using .Net library :
var result = System.IO.Path.GetFileNameWithoutExtension("https://mhnkp2.com/src/vid/pra/Tentang_Mata-Cerita_Boneka-Prasekolah.mp4")
Why you don't get only string started from last /
string s = "https:\/\/mhnkp2.com\/src\/vid\/pra\/Tentang_Mata-Cerita_Boneka-Prasekolah.mp4";
string lastSegment = s.Split('/').Last();
so, you can get Tentang_Mata-Cerita_Boneka-Prasekolah.mp4
and remove l'extension to get file name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.