Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

remove space from string

chen__kafty
Beginner
2,677 Views
Hello,

I am using Intel Fortran 2011to get a folder path, I got a problem when usedAPI function SHGetFolderPath. The problem is that I got a string like "C:\\....\\My Documents , I want to use trim() function to remove the space but failed, it seemed not space. could you help to solve the problem?

Thanks,
Kafty
0 Kudos
9 Replies
netphilou31
New Contributor III
2,677 Views
Hi,

I think that the SHGetFolderPath returns a C string (i.e. with a null character at the end), The best way to find where the string terminates is to use the index() function like in the sample code below:

length = index(Document, char(0)) - 1

and instead of using the trim() function, you can retrieve the substring by:

Document(1:length)

Regards,

Phil

0 Kudos
Les_Neilson
Valued Contributor II
2,677 Views
Kafty,
TRIM removes trailingspaces not embeded spaces like in"My Documents".
If you wish to remove embedded spaces then you must write a routine to do it BUT of course the result won't be the actual path.
For example you could use "index" and loop to find each space and adjust the string to remove it or replace the space with something else (e.g. underscore)

Les
0 Kudos
Steven_L_Intel1
Employee
2,677 Views
Kathy, is the problem that when you use the path in a generated command line that it gets treated as two or more arguments? The way to deal with this is to enclose the path in "".
0 Kudos
chen__kafty
Beginner
2,677 Views
Les,

Thanks, I only want to remove tailing spaces. what I guess that is not space but string characterin behind of the string. so the Trim function does not work

Kafty
0 Kudos
netphilou31
New Contributor III
2,677 Views
Kafty,

I have experienced the same strange behavior with API functions that returns a NULL terminated string. In this case it seems that the trim() function does not work properly and that the only way to get the correct lenght is to find the NULL character in the string (by using the index() function) as mentioned in my previous reply.

Regards,

Phil.
0 Kudos
anthonyrichards
New Contributor III
2,677 Views
To truncate the string, deleting everything from the first blank onwards (when searching from left to right in the string):

stringshort=stringlong(1:INDEX(stringlong,' ')-1)
0 Kudos
netphilou31
New Contributor III
2,677 Views
This works only if the string does not contains any blank inside !
0 Kudos
anthonyrichards
New Contributor III
2,677 Views
First define what you mean by 'the end of the string'.

Fortran strings may be defined as ending 'at the character before a continuous run of blank characters up to the last byte in the string'. The TRIM function will deal with this type of string.

C-strings are terminated by one, but sometimes 2, null characters (CHAR(0)).
So the first occurrence of a null character in the string, searching from left to right, will be where the C-string is supposed to end - all characters after that may be rubbish.
If you have a C-string, and there is a run of one or more blank characters up to this first NULL character, and you want to terminate the string at the start of this run of blanks, you need to do a TRIM on the string that ignores the bytes from the first null character onwards, left to right.

so

stringshort = TRIM( cstring( 1:INDEX( cstring,CHAR(0) )-1 ) )

If you decide that the strings that you are interested in are 'terminated' with a particular character CHAR(N), then just substitute CHAR(n) in the above.
0 Kudos
chen__kafty
Beginner
2,677 Views

Hello,

Thanks for all of your help. Phil's solution can help me to solve the problem.

Thanks a lot.
Kafty

0 Kudos
Reply