Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing the last character from a string 1

Status
Not open for further replies.

tsp120

Programmer
May 21, 2003
52
US
Hi, I'm working on a program where a user inputs a directory name when the program is run. Sometimes when a user inputs the directory they leave a "/" at the end of it. I would like to catch this instance and remove the backslash.

So for example someone might input:

/home/examples/testDir/

This will get stored into variable: $l

I would like to find that last "/" and remove it.

As a side note, I would also like to find all instances where someone does not input a "/" at the beginning of the directory and add the "/". I'm sure if someone can help me with one of those situations I can figure out the other.

Thanks in advance.
 
Code:
$dir='/home/examples/testDir/';
if (substr($dir, -1) eq '/') {
   print "success\n";
} else {
   print "failure\n";
}
HTH
--Paul

cigless ...
 
I understand what you're doing. However, if the condition is true, I want to delete that specific "/".

I see that substr($dir, -1) will find the last character, however, how would I find the first character for the other condition I want to solve?

Hope my questions make sense. :)

Thanks.
 
Code:
$dir='/home/examples/testDir';
if (substr($dir, 0,1) ne '/') {
  $dir='/'.$dir;

for removing the last character, instead of
Code:
   print "success\n";
Code:
   chop $dir;

for removing the first character
Code:
   $dir=substr ($dir, 1);

HTH
--Paul

cigless ...
 
TIMTOWDTI - but not really recommended:
Code:
{
   local $/ = '/';
   chomp $dir;
}
 
Why not just use regular expressions?

Code:
$dir =~ s!^([^/])!/\1!;
$dir =~ s!/$!!;
 
ok what am I doing wrong. I have this block of code:

______________________
if(substr($firstDir, 0, 1) ne '/')
{
$firstDir = '/'.$firstDir;
print"the first character is not a backslash\n";
}

if(substr($firstDir, -1) eq '/')
{
print"the last character is a backslash\n";
chop $firstDir;
}
_______________________

No matter what the program goes into both of those conditions (meaning they are always true), however it never makes the changes when it is in there. I'm really confused.

Thanks again :)
 
nevermind, I'm an idiot. The variable name is $firstdir not $firstDir. :)

It works great now. Thanks everyone.
 
That's the kind of error that should prompt you to be using strict and warnings in your code. If you had warnings turned on, it would have warned you that you were doing comparisons on empty variables ($firstDir). If you had strict turned on, you'd have predeclared $firstdir earlier so your program would have died and told you that $firstDir hadn't been declared earlier.

Using them may take a little more time in the short-term, while you're getting used to them but, in the long run, they'll save you tons of debugging time.
 
* 4 ish, well spotted, and vented ;-)

cigless ...
 
Why not just use regular expressions?

You could use regexp's, but you should use other faster alternatives whenever possible. substr() and chop() and index() and other string operators are much faster than regexp's.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top