Reputation: 25
Updated: I have my program reading a json file and after it reads it spits out several lines from the file. One of them being "org.apache.commons:commons-compress:1.8.1". I need a way to replace all the "." before the ":" with "\" as well as replace all the ":" with "\" This should create something that look like this "org\apache\commons\commons-compress\1.8.1" which would then be used to create the directory. For example:
Directory.CreateDirectory("org\\apache\\commons\\commons-compress\\1.8.1");
Original: I have my program reading a json file and after it reads it spits out several lines from the file. One of them being org.apache.commons:commons-compress:1.8.1 this text is then used to make a folder system. However there is a problem when trying to change the given text into a folder to be created, the end product should be "org\apache\commons\commons-compress\1.8.1". But from what I have seen you can only change all of the "." So what I am looking for is a way to only change the "." before the ":" and then change the ":" to "\" as well. Thanks for the help in advance, if you need me to clarify anything just ask.
Upvotes: 1
Views: 61
Reputation: 7880
This is a way to do what you need:
string source = @"org.apache.commons:commons-compress:1.8.1";
int lastColon = source.LastIndexOf(':') + 1;
string path = source.Substring(0, lastColon).Replace('.', '\\').Replace(':', '\\');
path += source.Substring(lastColon);
Console.WriteLine(path);
It looks for the position of the last colon, replaces the necessary characters up to that point (the +1 is to include this colon) and then just adds the remaining characters unchanged.
If you don't want to hardcode the \
, you can use System.IO.Path.DirectorySeparatorChar
instead.
Upvotes: 2