469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Removing a backslash from a string

Hi, I want to know how to use split function to split the string based on backslash(\).
Actually backslash is appearing at the end of the string. and i want to remove it.
so if anyone could guide me as to what will be the syntax for removing backslash(\) from the string??
Mar 25 '07 #1
5 12388
miller
1,089 Expert 1GB
You could use a regex, substr, chop, many different techniques to remove a single defined character from the end of a string. It sounds like a regex might be the simpliest for your purposes, but I can't say for sure as you haven't told us much:

Expand|Select|Wrap|Line Numbers
  1. my $string = "test string\\";
  2. print "Before = '$string'\n";
  3. $string =~ s/\\$//;
  4. print "After = '$string'\n";
  5.  
- Miller
Mar 25 '07 #2
Thanks a lot, Miller.
I will try out on what you have guided me.

Actually, i am new to perl and right now i know very little abt perl So that's why i
may not have been able to communicate to you properly. But you have guessed it right.
But can u plz tell me that code snippet that you have written - is it for those two
(\) only becoz my string changes dynamically
that means it can have
my $string = "test string\\";
or this
my $string = "test string\\\";
or it can be this even
my $string = "test string\\\\";

That's why i was thinking of Split function. so that whereever it matches \ it stores the values in an array and i can mak use of only the desired 0th array
element.

Regards, Shalini
Mar 25 '07 #3
KevinADC
4,059 Expert 2GB
what you need to use is a quantifier

Expand|Select|Wrap|Line Numbers
  1. $string =~ s/\\+$//; 
Mar 25 '07 #4
miller
1,089 Expert 1GB
That's why i was thinking of Split function. so that whereever it matches \ it stores the values in an array and i can mak use of only the desired 0th array
element.
Hi Shalili,

I understand your approach, but it's not even close to the best method. One thing you'll learn pretty quickly about perl is that it is excellent at text processing. You can almost always accomplish something with a direct method instead of something indirect like using split. Split is used for just what it says, splitting a string into an array. Unless that's really what your ultimate goal is, you best bet is to stick with a regular expression.

Kevin's example spells out exactly what you'll need. Use it, and if you find the time, try to understand it as well. Start reading perldoc and the introductions to regular expressions. It's information that will serve you very well.

- Miller
Mar 26 '07 #5
Hi

Thanks for the help..
Yes,I have started reading perldoc..and its really informative.

-shalini
Mar 26 '07 #6

Post your reply

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

Similar topics

2 posts views Thread by matt | last post: by
3 posts views Thread by Terry Asher | last post: by
3 posts views Thread by lino | last post: by
6 posts views Thread by John Salerno | last post: by
3 posts views Thread by Stef Mientki | last post: by
2 posts views Thread by Tobiah | last post: by
8 posts views Thread by =?Utf-8?B?YW5kcmV3?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.