473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Truncating Strings--an Expert needs novice help!

RedSon
5,000 Expert 4TB
Here is the problem:

I have a path to a file like \Windows\Start Menu\Accessories\Calculator.lnk and I am trying to truncate it down to just "Calculator".

Expand|Select|Wrap|Line Numbers
  1. _tcscpy(pItem->label, recentItem->path);
  2. *wcsrchr (pItem->label, '.') = '\0';
  3.  
First I am copying the recentItem (which is just a struct that has a TCHAR array called path -- which is the path to the shortcut) path to the pItem (which is just a struct that my WinMain method uses to populate a popup menu, that contains a label and a path -- where label is like "Calculator"). Then I truncate the label to remove the .lnk, so I am left with \Windows\Start Menu\Accessories\Calculator. Now I want to get rid of everything up to the last back-whack. Any ideas? I'm using all Win32 stuff here.
Feb 7 '07 #1
3 1289
Ganon11
3,652 Expert 2GB
You have the path in a string/character array, correct?

Then just search backwards through the array for the 'first' occurrence of the \ character - this will be the position of the last \ character in the string. Then get the substring from that position + 1 to the end of the string.
Feb 7 '07 #2
RedSon
5,000 Expert 4TB
You have the path in a string/character array, correct?

Then just search backwards through the array for the 'first' occurrence of the \ character - this will be the position of the last \ character in the string. Then get the substring from that position + 1 to the end of the string.
Right, but what is the methods for that?
Feb 7 '07 #3
Ganon11
3,652 Expert 2GB
Assuming your path is in a string called fpath,

Expand|Select|Wrap|Line Numbers
  1. int position;
  2. for (int i = fpath.length() - 1; i >= 0; i--) {
  3.    if (fpath[i] == '\\') // Because \ is escape sequence generator, and \\ is required to print \
  4.       position = i;
  5.       break;
  6.    }
  7. }
  8.  
  9. // Position now holds the location of the last '\' in fpath.
  10. fpath = fpath.substr(position + 1);
Since you don't want to include the \ in your new filepath, you add 1 to the position is in in to get the entire string after it.
Feb 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ben | last post by:
The page below isn't picking up details or location. What have I missed? Thanks in advance, Ben ____________________________________________ <?PHP
5
by: cyber-arkitect | last post by:
Hello all: I am a Solaris Unix Administrator with 6+ yrs. of experience in major companies who also has experience with AIX. Since I am good at Unix I would like to move into Oracle world,...
1
by: Patrick Parks | last post by:
hi guys, i am trying to read in CD info from a file and put it into an array, using a CD class. I have been working here for about 6-8 hours trying to get the input file into an array. i just need...
0
by: A | last post by:
Hi Group, I am trying to find how to insert a blank combo box option in my list of available choices thus representing no choice. I have a total hack right now below DataTable dt =...
2
by: Kovan | last post by:
Hi everyone, I do apologize in advance for posting this here, but I thought it might interest some people. I am thinking I should release the source code for iCodeLibrary.NET so that others...
1
by: dreamlab | last post by:
Hello, Can one of you javascript wizards help out a newbie, please? I’ve got a formHandler that is supposed to check for a good email address and name in the form after clicking the submit...
7
by: gheissenberger | last post by:
HELP! Guy who was here before me wrote a script to parse files in Python. Includes line: print u where u is a line from a file we are parsing. However, we have started recieving data from...
0
by: gcarr | last post by:
I need to double space the following code what is the easiest way to do this? <ul> <li> line one </li> <li> line two </li> <li> line three </li> </ul> Novice that I am I was using...
3
by: milov | last post by:
Project to do simulation testing (me teacher). Page one writes in real time to page two...both displayed at sam time with frames. Code below. Problem...I want to keep score. Each choice needs a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.