I have a native code snippet that I am going to write in managed code instead.
The native snippet code look like this:

std::string RClammer = "]";
std::string BigLine = "aaaaaaaa]aaaa";
int pos = 0;

while(std::string::npos != (pos = BigLine.find(RClammer, pos)))
{
	 BigLine.insert(pos + 1, " ");
	 pos = pos + 2;
}

My attemt to write that in managed code look like below but the application just "freeze" when I run this code, so I might beleive I am doing something wrong but dont know all logics of what I am doing excactly ?
I think it has to do something with ::npos that I dont get right ?

String^ RClammer = "]";
String^ BigLine = "aaaaaaaa]aaaa";
int pos = 0;

while(BigLine->Length != (pos = BigLine->IndexOf(RClammer, pos)))
{
        BigLine.Insert(pos + 1, " ");
        pos = pos + 2;
}

Okay, perheps my attempt is not correct. The native code works fine.

What could be a correct way to write this native code for managed. I am not sure of what I will do except for what I have tried.

Edited 7 Years Ago by Darth Vader: n/a

Maybe something like this?

String^ RClammer = "]";
String^ BigLine = "aaaaaaaa]aaaa";
int pos = 0;

while ((pos = BigLine->IndexOf(RClammer, pos)) != -1)
{
    BigLine = BigLine->Insert(++pos, " ");
}

Do not forget that managed strings are immutable. The Insert method does not modify the string, it returns a new one.

This question has already been answered. Start a new discussion instead.