Get arguments and file name from specific path - c#

I'm trying to extract argument and file name from path like below:
C:\Users\user\Desktop\foo.exe foo://action/bar
I tried to use Path.GetFileName but since argument contains directory separators, it returns bar instead of foo.exe
Is there any way to get argument and file name?

You can get the command line argument from the string [] args passed to the Main method.
Or you can use the static method Environment.GetCommandLineArgs https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx

Use LastIndexOf to reverse-search the string for the backslash, then Substring to grab everything beyond that:
int i = path.LastIndexOf(#"\");
return (i > -1 && i < path.Length) ? path.Substring(i + 1) : string.Empty;
If you need to separate the filename and argument, use IndexOf to look for the space or Split the result on the space character.

Related

replace function- No overload for this method takes 2 arguments

I am trying to perform a string replace, but it results in:"No overload for method Replace accepts 2 arguments."
What I need to do:
fetch string(usernames) separated by "." and store only the 1st half.
input can be eu.rails.io or in.rails.io, or fr.rails.io
desired result eu, in, fr
geo = geo.Replace(".rails.io", "");
(declared this variable geo earlier)
//results in error— "No overload for the method Replace takes 2 arguments"
(edited with 'geo'var)
Edit: If the Replace Method has an error, the variable geo might not be a string.
Multiple possibilities. You could replace the part ".rails.io" as you suggested:
string geo = "eu.rails.io";
geo = geo.Replace(".rails.io", string.Empty);
You could also split the string at the point, and only use the first part (you need to be sure, that the string you want to keep doesn't contain a dot):
string geo = "eu.rails.io";
geo = geo.Split('.')[0];

How to remove character from string?

I have a string which is getting from a userInput. What I want to do now is removing a unique character from this string but only remove it once. The main problem is that this unique character doesn't have a unique index. For example:
User has input a string like : "0123456", and now I want to remove the first '1',so the string will be output like "023456". How ever, if a user input a string like "01123456", how can I remove the first '1' and make it looks like "0123456"? I am looking for a method that can be used for both of situation. I was using string.TrimStart(), but doesn't get what I want. How can I do this?
You could use Remove and IndexOf.
var str = "01123456";
var i = str.IndexOf('1'); // IndexOf returns -1 when there is no element found, so we need to handle that when calling remove.
var res = (i >= 0) ? str.Remove(i, 1) : str;
Console.WriteLine(res); // 0123456
I think you what you need is string.Remove method. See MSDN documentation here: https://learn.microsoft.com/en-us/dotnet/api/system.string.remove?view=netframework-4.7.2#System_String_Remove_System_Int32_System_Int32_
If you don't know where is your character, at first call string.IndexOf to find it. If this call returns nonnegaive number, call Remove to remove it. Just note that string is immutable so it will always create a new object.
yourstring = yourstring.IndexOf('1') != -1 ? yourstring.Remove(yourstring.IndexOf('1'), 1) : yourstring;
Another way would be to use a combination of Contains, Remove, and IndexOf:
if (userInput.Contains('1')) userInput = userInput.Remove(userInput.IndexOf('1'), 1);
Or if you want to be Linq-y...
userInput = string.Concat(userInput.TakeWhile(chr => chr != '1')
.Concat(userInput.SkipWhile(chr => chr != '1').Skip(1)));

Why doesn't IndexOf work with StringCompare?

I get this error
string v = "aeiou";
foreach(int i in lokacija.Naziv) {
if(v.indexOf(lokacija.Naziv[i], StringComparison.CurrentCultureIgnoreCase) = -1)
s+=lokacija.Naziv[i];
}
The error says "cannot convert from System.StringComparison to int". But I know there is an overload of the method indexOf(string) which accepts arguments of the type StringComparison. So how can I resolve this?
First of all, you should be using == for comparison.
Second, all IndexOf overloads whose first parameter is a char, their second parameter is an int. That's why you get that error. In order to use the overload that receives a StringComparison, make that first parameter a string, like this:
if (v.indexOf(lokacija.Naziv[i].ToString(), StringComparison.CurrentCultureIgnoreCase) == -1)
BTW, are you trying to remove vowels from a string? I recommend you try this.
you loop seems strange... did you mean this?
foreach(string ssub in lokacija.Naziv) {
if(v.indexOf(ssub, StringComparison.CurrentCultureIgnoreCase) = -1)
s+=ssub;
#Johnathan-Lonowski hit the nail on the head- you are getting this version of String.IndexOf, not this one, because you are looking for a character in a string, not an occurrence of one string in another.

substring from specific point of the string(from reverse side )

I wanted to substring from special point.
abcdef.png
I want
.png
Here i tried
string str = "abcdef.png";
str = str.Substring(0, str.Length - 4);
but then only shows the abcdef only BUT i want .png part
Just use the overload which takes a single parameter - the start point:
str = str.Substring(str.Length - 4);
Or better, use a method designed to get the extension of a filename - Path.GetExtension:
string extension = Path.GetExtension(str);
You can use Path.GetExtension method instead of substring.
string str = "abcdef.png";
string ext = Path.GetExtension(str); // .png
It seems you're dealing with file names, Use Path.GetExtension method for this purpose.
You need to pass str.Length - 4 as the first (and only) parameter, not as the second parameter:
str = str.Substring(str.Length - 4);
The way your code had it, you got a substring starting at zero, and containing str.Length - 4 characters.
If you want to take just the dot and the extension, use
str = str.Substring(str.LastIndexOf('.'));
expression.
If you want the extension of a filename use Path.GetExtension(str). Much easier.
http://msdn.microsoft.com/en-us/library/system.io.path.getextension(v=vs.110).aspx
try below code it will return the extension of file.
string extension = Path.GetExtension(str);

How to remove end of string (fileName) using substring?

I know I must use Substring to remove, but I dont know how to do this. I need to remove end of string like this
from
"C:\\Users\\myname\\Pictures\\shoeImage.jpg"
to
"C:\\Users\\myname\\Pictures"
Use the methods of the System.IO.Path class instead, in specific GetDirectoryName.
You can use Path.GetDirectoryName method.
Returns the directory information for the specified path string.
Console.WriteLine(Path.GetDirectoryName("C:\\Users\\myname\\Pictures\\shoeImage.jpg"));
It returns this;
C:\Users\myname\Pictures
Here a DEMO.
With String.SubString method, you can use it like;
string path = "C:\\Users\\myname\\Pictures\\shoeImage.jpg";
Console.WriteLine(path.Substring(0, path.LastIndexOf(#"\")));
You should use FileInfo in such scenarios -
FileInfo info = new FileInfo("C:\\Users\\myname\\Pictures\\shoeImage.jpg");
string name = info.DirectoryName;
OR
Path.GetDirectoryName("C:\\Users\\myname\\Pictures\\shoeImage.jpg");
If you want to substring it:
var subString = yourString.SubString(0, yourString.LastIndexOf('\\'));

Categories