Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

end of string found

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 23 May 2008 17:12 GMT
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
Gilles Kohl [MVP] - 23 May 2008 17:51 GMT
>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:
>
[quoted text clipped - 17 lines]
>
>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.
Ignacio Machin ( .NET/ C# MVP ) - 23 May 2008 19:06 GMT
> IndexOf gives you the start of the string found in another string.  How do
> you get the end of the string found?
[quoted text clipped - 24 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.