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;
}

Recommended Answers

All 4 Replies

IndexOf() returns -1 if it cannot find the value.

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.

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.

thank you, your code worked fine. I will check this out.

thanks again !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.