How to remove spaces before and after in the specified path?

Hello!

How to remove spaces before and after in the specified path?

I have a string value getting like this - "C: \ Test file \ Temp1"

My question is remove spaces before and after (colon & Backslash): \ only (not in text)

Expecting output :"C:\Test file\Temp1"

I have written code like below
1
2
3
4
5
6
7
8
9
10
11
12
path = L"C: \ Test file \ Temp1";
int length = path.length();

for (int i = 0; i < length; i++)
{
   if (path [i] == ': ' || '\ ')
   {
	path .erase(i, 1);
	length--;
        i--;
   }
}



how can i solve this issue ? can help someone.
Last edited on
This may give you an idea:
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>

int main()
{
  std::string path = "C: \\ Test file \\ Temp1";

  std::string::size_type pos;
  while((pos = path.find(" \\")) != std::string::npos)
  {
    path.erase(pos, 1);
  }
}
Note that for "\ " it is similar. You just need + 1 on pos.
From the example given, need to replace " \ " with just "\". So consider:

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <iostream>

int main()
{

	std::string path = "C: \\ Test file \\ Temp1";

	for (std::string::size_type pos = 0; (pos = path.find(" \\ "), pos) != std::string::npos; path.replace(pos, 3, "\\"));

	std::cout << path << std::endl;
}


which displays:


C:\Test file\Temp1


as required.

Note that if there is more than one space, or sometimes before only or after only - then a different approach is needed.
Last edited on
So my example removes all leading spaces. Here is how to remove all trailing spaces as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main()
{
  std::string path = "C:               \\         Test file            \\            Temp1";

  std::string::size_type pos;
  while((pos = path.find(" \\")) != std::string::npos)
  {
    path.erase(pos, 1);
  }
  while((pos = path.find("\\ ")) != std::string::npos)
  {
    path.erase(pos + 1, 1);
  }

  std::cout << path << std::endl;

  return 0;
}
Output:
C:\Test file\Temp1
There are two loops because there might be backslashes without leading and/or trailing spaces. So this works in all cases.
Last edited on
Thanks you so much @coder777

it's working all the cases leading and trailing spaces.

is there any way to both the cases in single while loop leading and trailing cases?
is there any way to both the cases in single while loop leading and trailing cases?
Yes, but that requires more attention.

You need a start_pos. Set it to 0.

Search for the backslash only using start_pos (as seeplus showed).

Set bool found_space = false;

Within the loop check whether theres is a trailing (pos + 1) space. If so remove it, set found_space = true. Note that before checking the space you need to check whether the backslash isn't the last character.

Similar for the leading backspace:

Within the loop check whether theres is a leading (pos - 1) space. If so remove it, set found_space = true. Note that before checking the space you need to check whether the backslash isn't the first character.

When found_space == false set start_pos = pos + 1;

This is a good execise for you to check your understanding of the subject...
Last edited on
Hi coder777

I tried with in same loop to remove leading and trailing spaces of given path but i'm not getting expected output please help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main()
{
  std::string path = "C:               \\         Test file            \\            Temp1";
  stringx::size_type pos = 0;
  bool found_space = false;
  while ((pos = path.find("\\")) != stringx::npos)
  {
	if (pos == path.find(" \\"))
	{
	     path.erase(pos, 1);
	     found_space = true;
	}
        else if (pos == path.find("\\ "))
	{
             path.erase(pos+1, 1);
	     found_space = true;
	}
   }
}
Last edited on
Okay, you did not get what told you. I have a simplier solution not so far from your try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
  std::string path = "C:               \\         Test file            \\            Te\\ mp1";

  std::string::size_type pos0 = std::string::npos;
  std::string::size_type pos1 = std::string::npos;
  do
  {
    pos0 = path.find(" \\");
    if(pos0 != std::string::npos)
      path.erase(pos0, 1);
    pos1 = path.find("\\ ");
    if(pos1 != std::string::npos)
      path.erase(pos1 + 1, 1);
  }
  while((pos0 != std::string::npos) || (pos1 != std::string::npos));

  std::cout << path << std::endl;

  return 0;
}
Slightly different approach where the string is only iterated once and erases all the non-required consecutive spaces together, which for large strings will give a small performance benefit. Consider:

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
#include <string>
#include <iostream>

int main()
{
	std::string path = "\\  C: \\     Test   file    \\  Temp1\\ ";

	for (size_t pos = 0; (pos = path.find('\\', pos)) != std::string::npos; ++pos) {
		if (pos > 0)
			if (const size_t fl = path.find_last_not_of(' ', pos - 1); fl == std::string::npos) {
				path.erase(0, pos);
				pos = 1;
			} else {
				path.erase(fl + 1, pos - fl - 1);
				pos = fl + 2;
			}

		if (auto ff = path.find_first_not_of(' ', pos); ff == std::string::npos)
			path.erase(pos, ff);
		else
			path.erase(pos, ff - pos);
	}

	std::cout << "'" << path << "'" << std::endl;
}

Topic archived. No new replies allowed.