Hey everyone, |
Re: Removing space at end of each line from a text attribute |
Re: Removing space at end of each line from a text attribute
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 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)));
}
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 DOORSUser - Wed Jan 18 09:20:23 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)
|
Re: Removing space at end of each line from a text attribute DOORSUser - Wed Jan 18 09:20:23 EST 2012
In that case you could still use the rightTrim() function but you need to apply it to each line:
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();
|
Re: Removing space at end of each line from a text attribute 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)
Thank you so much for your help Tony and Fu Lin Yiu!! |
Re: Removing space at end of each line from a text attribute 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:
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();
|