Removing space at end of each line from a text attribute

Hey everyone,

I was just wondering if anyone knew the command to remove spaces at end of each line from a text attribute? I know I read it somewhere in the DXL Reference manual, but I can't seem to find it anymore. If anyone could help that would be great. Thanks!
DOORSUser - Tue Jan 17 14:56:07 EST 2012

Re: Removing space at end of each line from a text attribute
rain_bow - Tue Jan 17 15:53:39 EST 2012

Search for trim on this forum.

Re: Removing space at end of each line from a text attribute
SystemAdmin - Tue Jan 17 16:47:28 EST 2012

These work for me:
 

string rightTrim(string s) {
    if (null(s)) return "";
    int first = 0;
    int last = length(s) - 1;
    while( (last > -1) && (isspace(s[last])) ){
       last--;
    }
    return(s[first:last]);
}
 
string leftTrim(string s) {
    if (null(s)) return "";
    int first = 0;
    int last = length(s) - 1;
    while( (isspace(s[first])) && (first < last) ) {
       first++;
    }
    return(s[first:last]);
}
 
 
string trim(string s) {
    return(leftTrim(rightTrim(s)));
}

Re: Removing space at end of each line from a text attribute
DOORSUser - Wed Jan 18 09:20:23 EST 2012

SystemAdmin - Tue Jan 17 16:47:28 EST 2012

These work for me:
 

string rightTrim(string s) {
    if (null(s)) return "";
    int first = 0;
    int last = length(s) - 1;
    while( (last > -1) && (isspace(s[last])) ){
       last--;
    }
    return(s[first:last]);
}
 
string leftTrim(string s) {
    if (null(s)) return "";
    int first = 0;
    int last = length(s) - 1;
    while( (isspace(s[first])) && (first < last) ) {
       first++;
    }
    return(s[first:last]);
}
 
 
string trim(string s) {
    return(leftTrim(rightTrim(s)));
}

Thank you so much for the reply.

The function works but it only removes the space and newlines at the end of the string. My problem was that I have a multiple line text, at the end of each line, there was a space and I'd like to remove these spaces. Any help would be appreciated!

Re: Removing space at end of each line from a text attribute
Tony_Goodman - Wed Jan 18 11:43:50 EST 2012

DOORSUser - Wed Jan 18 09:20:23 EST 2012
Thank you so much for the reply.

The function works but it only removes the space and newlines at the end of the string. My problem was that I have a multiple line text, at the end of each line, there was a space and I'd like to remove these spaces. Any help would be appreciated!

Try this.
 

Buffer b = create
 
void removeSpaces(Object o)
{
    int i = 0
        string s = o."Object Text" ""
        int len = length(s)
        
        setempty(b)
        
        if (len < 2) return
        
        for (i = 0; i < len-1; i++)
        {
                if (s[i:i+1] != " \n")
                {
                        b += s[i]
                }
        }
        b+= s[len-1]
        
        o."Object Text" = tempStringOf(b)
}
 
 
Object o = current Object
removeSpaces(o)

 


Tony Goodman, www.smartdxl.com

 

Re: Removing space at end of each line from a text attribute
SystemAdmin - Wed Jan 18 13:04:03 EST 2012

DOORSUser - Wed Jan 18 09:20:23 EST 2012
Thank you so much for the reply.

The function works but it only removes the space and newlines at the end of the string. My problem was that I have a multiple line text, at the end of each line, there was a space and I'd like to remove these spaces. Any help would be appreciated!

In that case you could still use the rightTrim() function but you need to apply it to each line:

This code would handle it in an abstract way. It employes a split function. You split on newlines, and then apply another function (rightTrim in your case) to the output from split.

Many processing issues can be solved with this divide and conquer approach.
 

string rightTrim(string s) {
    if (null(s)) return (string null());
    int first = 0;
    int last = length(s) - 1;
    while( (last > -1) && (isspace(s[last])) ){
       last--;
    }
    return(s[first:last]);
}
 
string leftTrim(string s) {
    if (null(s)) return (string null());
    int first = 0;
    int last = length(s) - 1;
    while( (isspace(s[first])) && (first < last) ) {
       first++;
    }
    return(s[first:last]);
}
 
string trim(string s) {
    return(leftTrim(rightTrim(s)));
}
 
 
void skSplitOn(char ch, Buffer b, Skip skSegments) {
    bool hasMoreThanOne = false;
    int indexOfSeparator = -1;
    int firstSep = -1;
    int lastSep = 0;
    int counter = 0;
    if ((!null(b)) && (length(b) > 0)) {
        while (lastSep != length(b)) {
            int startingIndex = firstSep + 1;
            lastSep = contains(b, ch, startingIndex)
            if (lastSep == -1) {
                lastSep = length(b);
            }
            string mySeqmentStr = b[ startingIndex : (lastSep-1) ];
            if (length(mySeqmentStr) > 0) {
                counter++;
                put(skSegments,           //-
                    (int counter),        //-
                    (string mySeqmentStr) );
            }
            firstSep = lastSep;
        } // while
        if (counter == 0) {
            // Case of no separator -- return the whole buffer
            put(skSegments,   //-
                (int 0),      //-
                (stringOf(b)) );
        }
    }
}
 
void rightTrimEachLine(string strOriginal, Buffer bRightTrimmed) {
    Buffer bOriginal = create();
    if (null(bRightTrimmed)) {
        ack "Null buffer passed in"
        halt;
    }
 
    bOriginal += strOriginal;
 
    Skip skOriginal = create();
 
    skSplitOn('\n', bOriginal, skOriginal);
    string str;
    int i = -1;
    for str in skOriginal do {
        i++;
        // <line> for illustration only
        // WARNING: I hate this DXL "feature" but you must put extra () around a
        //          function that takes a string as a parameter if there is a string
        //          to the right of it due to unusual operator precedence.
        bRightTrimmed += "<line" (i) ">" (rightTrim(str)) "</line" (i) ">" "\n";
    }
    delete(bOriginal);
    delete(skOriginal);
}
 
void main() {
    Buffer bRightTrimmed = create();
 
    //------------------------------------------------------------------------------
    // Typical
    //------------------------------------------------------------------------------
    rightTrimEachLine("    aaaaa   \n  bbbbbb \n    cccccc\n", //-
                      bRightTrimmed);
    print("bRightTrimmed:\n" stringOf(bRightTrimmed) "\n");
    setempty(bRightTrimmed)
 
    //------------------------------------------------------------------------------
    // Empty
    //------------------------------------------------------------------------------
    rightTrimEachLine((string null()), //-
                      bRightTrimmed);
    print("bRightTrimmed:\n" stringOf(bRightTrimmed) "\n");
    setempty(bRightTrimmed)
 
 
    //------------------------------------------------------------------------------
    // One line
    //------------------------------------------------------------------------------
    rightTrimEachLine(" One line no newline   ", //-
                      bRightTrimmed);
    print("bRightTrimmed:\n" stringOf(bRightTrimmed) "\n");
    setempty(bRightTrimmed)
 
    //--------------------------------------------------------------------------
    // Cleanup
    //--------------------------------------------------------------------------
    delete(bRightTrimmed);
}
 
main();

 


OUTPUT:

bRightTrimmed:
<line0> aaaaa</line0>
<line1> bbbbbb</line1>
<line2> cccccc</line2>

bRightTrimmed:

bRightTrimmed:
<line0> One line no newline</line0>

 

Re: Removing space at end of each line from a text attribute
DOORSUser - Wed Jan 18 14:01:40 EST 2012

Tony_Goodman - Wed Jan 18 11:43:50 EST 2012

Try this.
 

Buffer b = create
 
void removeSpaces(Object o)
{
    int i = 0
        string s = o."Object Text" ""
        int len = length(s)
        
        setempty(b)
        
        if (len < 2) return
        
        for (i = 0; i < len-1; i++)
        {
                if (s[i:i+1] != " \n")
                {
                        b += s[i]
                }
        }
        b+= s[len-1]
        
        o."Object Text" = tempStringOf(b)
}
 
 
Object o = current Object
removeSpaces(o)

 


Tony Goodman, www.smartdxl.com

 

The script has worked beautifully. As the script looks for the enter key and removes the space before it, the space at the end of last line was ignored. I used Fu Lin Yiu's function to remove the space at the end of the last line.

Thank you so much for your help Tony and Fu Lin Yiu!!

Re: Removing space at end of each line from a text attribute
DOORSUser - Wed Jan 18 14:17:19 EST 2012

SystemAdmin - Wed Jan 18 13:04:03 EST 2012

In that case you could still use the rightTrim() function but you need to apply it to each line:

This code would handle it in an abstract way. It employes a split function. You split on newlines, and then apply another function (rightTrim in your case) to the output from split.

Many processing issues can be solved with this divide and conquer approach.
 

string rightTrim(string s) {
    if (null(s)) return (string null());
    int first = 0;
    int last = length(s) - 1;
    while( (last > -1) && (isspace(s[last])) ){
       last--;
    }
    return(s[first:last]);
}
 
string leftTrim(string s) {
    if (null(s)) return (string null());
    int first = 0;
    int last = length(s) - 1;
    while( (isspace(s[first])) && (first < last) ) {
       first++;
    }
    return(s[first:last]);
}
 
string trim(string s) {
    return(leftTrim(rightTrim(s)));
}
 
 
void skSplitOn(char ch, Buffer b, Skip skSegments) {
    bool hasMoreThanOne = false;
    int indexOfSeparator = -1;
    int firstSep = -1;
    int lastSep = 0;
    int counter = 0;
    if ((!null(b)) && (length(b) > 0)) {
        while (lastSep != length(b)) {
            int startingIndex = firstSep + 1;
            lastSep = contains(b, ch, startingIndex)
            if (lastSep == -1) {
                lastSep = length(b);
            }
            string mySeqmentStr = b[ startingIndex : (lastSep-1) ];
            if (length(mySeqmentStr) > 0) {
                counter++;
                put(skSegments,           //-
                    (int counter),        //-
                    (string mySeqmentStr) );
            }
            firstSep = lastSep;
        } // while
        if (counter == 0) {
            // Case of no separator -- return the whole buffer
            put(skSegments,   //-
                (int 0),      //-
                (stringOf(b)) );
        }
    }
}
 
void rightTrimEachLine(string strOriginal, Buffer bRightTrimmed) {
    Buffer bOriginal = create();
    if (null(bRightTrimmed)) {
        ack "Null buffer passed in"
        halt;
    }
 
    bOriginal += strOriginal;
 
    Skip skOriginal = create();
 
    skSplitOn('\n', bOriginal, skOriginal);
    string str;
    int i = -1;
    for str in skOriginal do {
        i++;
        // <line> for illustration only
        // WARNING: I hate this DXL "feature" but you must put extra () around a
        //          function that takes a string as a parameter if there is a string
        //          to the right of it due to unusual operator precedence.
        bRightTrimmed += "<line" (i) ">" (rightTrim(str)) "</line" (i) ">" "\n";
    }
    delete(bOriginal);
    delete(skOriginal);
}
 
void main() {
    Buffer bRightTrimmed = create();
 
    //------------------------------------------------------------------------------
    // Typical
    //------------------------------------------------------------------------------
    rightTrimEachLine("    aaaaa   \n  bbbbbb \n    cccccc\n", //-
                      bRightTrimmed);
    print("bRightTrimmed:\n" stringOf(bRightTrimmed) "\n");
    setempty(bRightTrimmed)
 
    //------------------------------------------------------------------------------
    // Empty
    //------------------------------------------------------------------------------
    rightTrimEachLine((string null()), //-
                      bRightTrimmed);
    print("bRightTrimmed:\n" stringOf(bRightTrimmed) "\n");
    setempty(bRightTrimmed)
 
 
    //------------------------------------------------------------------------------
    // One line
    //------------------------------------------------------------------------------
    rightTrimEachLine(" One line no newline   ", //-
                      bRightTrimmed);
    print("bRightTrimmed:\n" stringOf(bRightTrimmed) "\n");
    setempty(bRightTrimmed)
 
    //--------------------------------------------------------------------------
    // Cleanup
    //--------------------------------------------------------------------------
    delete(bRightTrimmed);
}
 
main();

 


OUTPUT:

bRightTrimmed:
<line0> aaaaa</line0>
<line1> bbbbbb</line1>
<line2> cccccc</line2>

bRightTrimmed:

bRightTrimmed:
<line0> One line no newline</line0>

 

Thank you so much for your help! I really appreciate it!