smart way to trim string

Hello
I am having a string - e. g.
std::string sTemp = "http://www.cplusplus.com/forum/post.cgi.1" From this I want to extract post.cgi , is there wany smart way to do that.I can try with sTemp.erase , but for this I will have to create unnecessary position variables ,indexes,iterators.
i think erase would be the shorted
1
2
3
4
5
6
std::string s("post.cgi");
std::string::size_type len = myStr.find(s);
if(len != std::string::npos)
{
      std::string str = myStr.erase(0, len).erase(s.length());
}
How do you know that "post.cgi" is the substring you want from that string? What I am getting at is that, based on your problem description, std::string substr = "post.cgi" does what you want.
Simple.

1
2
std::string sTemp = "http://www.cplusplus.com/forum/post.cgi.1";
sTemp.erase( sTemp.find_last_of( '.' ) );

This presumes, of course, that the string is valid. (In other words, it assumes that the string cannot be "foo.bar/baz".)

For a more robust solution, you need a little more code. The following is a simplified version of what I use for filename manipulations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
string ExtractDirectory( const string& path, char delimiter = '/' )
  //
  // Returns everything, including the trailing path separator, except the filename
  // part of the path.
  //
  // "/foo/bar/baz.txt" --> "/foo/bar/"
  {
  return path.substr( 0, path.find_last_of( delimiter ) + 1 );
  }

string ExtractFilename( const string& path, char delimiter = '/' )
  //
  // Returns only the filename part of the path.
  //
  // "/foo/bar/baz.txt" --> "baz.txt"
  {
  return path.substr( path.find_last_of( delimiter ) + 1 );
  }

string ExtractFileExtension( const string& path, char delimiter = '/' )
  //
  // Returns the file's extension, if any. The period is considered part
  // of the extension.
  //
  // "/foo/bar/baz.txt" --> ".txt"
  // "/foo/bar/baz" --> ""
  {
  string filename = ExtractFilename( path, delimiter );
  string::size_type n = filename.find_last_of( '.' );
  if (n != string::npos)
    return filename.substr( n );
  return string();
  }

string ChangeFileExtension( const string& path, const string& ext, char delimiter = '/' )
  //
  // Modifies the filename's extension. The period is considered part
  // of the extension.
  //
  // "/foo/bar/baz.txt", ".dat" --> "/foo/bar/baz.dat"
  // "/foo/bar/baz.txt", "" --> "/foo/bar/baz"
  // "/foo/bar/baz", ".txt" --> "/foo/bar/baz.txt"
  //
  {
  string filename = ExtractFilename( path, delimiter );
  return ExtractDirectory( path, delimiter )
       + filename.substr( 0, filename.find_last_of( '.' ) )
       + ext;
  }

This is, of course applicable to your URL:
1
2
3
std::string sTemp = "http://www.cplusplus.com/forum/post.cgi.1";
sTemp = ChangeFileExtension( sTemp, "" );
// sTemp == "http://www.cplusplus.com/forum/post.cgi" 

Hope this helps.
Topic archived. No new replies allowed.