Thursday, July 9, 2009

Java or C++: How to remove white space at the end of a line of text?

Is there a Java or C++ function that will do this? I found a java function String trim() in Java's String class, but apparently it gets rid of any space before the line's start, too, which is bad for indentation.





Thx.





(Also ... if you know of a language other than c++ or java that has such a function ... I'd be happy to give it a look.)

Java or C++: How to remove white space at the end of a line of text?
java


if you know how many space you will remove:


try


substring(beginIndex, endIndex)


beginIndex = 0 and endIndex = yourString.length() - numOfSpace
Reply:There arn't any such methods in Java standards String class ..but you can write a simple private method to do so. Use indexOf (..) and replace(..) functions .... good luck .... refer the following link to get some information
Reply:java





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


void cutline( void );


string line;


int main()


{


while( getline(cin, line)) {


cutline();


cout %26lt;%26lt; line %26lt;%26lt; endl;


}


return 0;


}





void cutline()


{


int i = line.size();





while( i-- %26gt;= 0 )


if( line[i] != ' ' %26amp;%26amp; line[i] != '\t' )


break;


line.resize(++i);


}

artificial flowers

No comments:

Post a Comment