end of string found

T

tshad

IndexOf gives you the start of the string found in another string. How do
you get the end of the string found?

I am trying to get the path of the startup:

string stemp = Application.StartupPath;

I then want to do something like:

stemp =
stemp.Substring(0,stemp.IndexOf(@"theFolder\")+String.length((@"theFolder\"))
+ "afile.txt";

So that I would end up with something like:

c:\folder1\theFolder\afile.txt

I could do this by putting "theFolder" in a string and then taking the lengh
of the string in my Substring call,

var s = @"theFolder\";
stemp = stemp.Substring(0,stemp.IndexOf(s)+s.Length)) + "afile.txt";

but wanted to see if there was an easier way?

Thanks,

Tom
 
G

Gilles Kohl [MVP]

IndexOf gives you the start of the string found in another string. How do
you get the end of the string found?

By adding the length of the search string, which is known.
I am trying to get the path of the startup:

string stemp = Application.StartupPath;

I then want to do something like:

stemp =
stemp.Substring(0,stemp.IndexOf(@"theFolder\")+String.length((@"theFolder\"))
+ "afile.txt";

So that I would end up with something like:

c:\folder1\theFolder\afile.txt

I could do this by putting "theFolder" in a string and then taking the lengh
of the string in my Substring call,

var s = @"theFolder\";
stemp = stemp.Substring(0,stemp.IndexOf(s)+s.Length)) + "afile.txt";

but wanted to see if there was an easier way?

There is for this type of task, use the methods of the Path class:
(add a "using System.IO;")

string stemp = Application.StartupPath;

string newFileName =
Path.Combine(Path.GetDirectoryName(stemp), "afile.txt");



Regards,
Gilles.
 
I

Ignacio Machin ( .NET/ C# MVP )

IndexOf gives you the start of the string found in another string.  How do
you get the end of the string found?

I am trying to get the path of the startup:

string stemp = Application.StartupPath;

I then want to do something like:

stemp =
stemp.Substring(0,stemp.IndexOf(@"theFolder\")+String.length((@"theFolder\"­))
+ "afile.txt";

So that I would end up with something like:

c:\folder1\theFolder\afile.txt

I could do this by putting "theFolder" in a string and then taking the lengh
of the string in my Substring call,

var s = @"theFolder\";
stemp = stemp.Substring(0,stemp.IndexOf(s)+s.Length)) + "afile.txt";

but wanted to see if there was an easier way?

Thanks,

Tom

Hi,

For working with files and folder you have a couple of classes like
Path , Directory and File
in your case Path.GetDirectoryName is what you are after I think
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top