ho stringhe che hanno una directory nel seguente formato:Ottenere contenuti dopo l'ultima barra
C: // // ciao mondo
come faccio a estrarre tutto dopo l'ultimo carattere/(mondo)?
ho stringhe che hanno una directory nel seguente formato:Ottenere contenuti dopo l'ultima barra
C: // // ciao mondo
come faccio a estrarre tutto dopo l'ultimo carattere/(mondo)?
string path = "C://hello//world";
int pos = path.LastIndexOf("/") + 1;
Console.WriteLine(path.Substring(pos, path.Length - pos)); // prints "world"
Il metodo LastIndexOf
esegue la stessa IndexOf
.. ma dalla fine della stringa.
Suggerirei di guardare lo spazio dei nomi System.IO
come sembra che si possa voler usare quello. C'è anche DirectoryInfo e FileInfo che potrebbero essere utili qui. Specificamente DirectoryInfo's Name property
var directoryName = new DirectoryInfo(path).Name;
C'è una classe statica per lavorare con tracciati chiamate Path
.
È possibile ottenere il nome file completo con Path.GetFileName
.
o
È possibile ottenere il nome del file senza estensione con Path.GetFileNameWithoutExtension
.
using System.Linq;
var s = "C://hello//world";
var last = s.Split('/').Last();
Prova questo:
string worldWithPath = "C://hello//world";
string world = worldWithPath.Substring(worldWithPath.LastIndexOf("/") + 1);
avevo pensato che, a meno di notare che il PO sembra non essere concentrandosi su un file, ma una directory –