0

I can't split a string with a variable:

string directoryName = Path.GetDirectoryName(Global.filepath);
string relativePath = ofd.FileName.Split(directoryName);

I get this error for directoryName: "Argument 1: cannot convert from 'string' to 'char'"

Has anyone an other idea? Thanks for the help.

1
  • 2
    Also, you can't store the results of a string.Split in a single string variable. Commented May 7, 2020 at 10:51

4 Answers 4

2

There is a specific overload you can use for this.

Try something like

ofd.FileName.Split(directoryName, StringSplitOptions.None);

or

ofd.FileName.Split(new string[] { directoryName }, StringSplitOptions.None);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this overloading of Split string Split(String[], StringSplitOptions)

var relativePath = ofd.FileName.Split(new string[] { directoryName}, StringSplitOptions.None);

Comments

0

Google is your friend.

https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netcore-3.1#System_String_Split_System_String_System_StringSplitOptions_

The above will help

string[] relativePath = ofd.FileName.Split(directoryName, StringSplitOptions.None);

Comments

0

Good start is as usual to read documentation. There you see many overloads of string.split method.

In your case you try to use

public string[] Split (string separator, StringSplitOptions options = System.StringSplitOptions.None);

but it return string[] not string

string[] path = ofd.FileName.Split(directoryName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.