Removing a backslash from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalini jain
    New Member
    • Mar 2007
    • 10

    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??
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    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:

    Code:
    my $string = "test string\\";
    print "Before = '$string'\n";
    $string =~ s/\\$//;
    print "After = '$string'\n";
    - Miller

    Comment

    • shalini jain
      New Member
      • Mar 2007
      • 10

      #3
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        what you need to use is a quantifier

        Code:
        $string =~ s/\\+$//;

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Originally posted by shalini jain
          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

          Comment

          • shalini jain
            New Member
            • Mar 2007
            • 10

            #6
            Hi

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

            -shalini

            Comment

            Working...