How to

iterate through text using fixed statement

Published: 16. January 2009 | Updated: 18. January 2009
License: Microsoft Public License (MS-PL)
Categories: Framework » Unsafe Code
Tags: C# Performance Unsafe
Was this snippet helpful for you? YESYES / NONO

Method

public static unsafe void UnderscoreSpaces(string text)
{
    fixed (char* c = text)
    {
        // iterate through text
        for (int i = 0; c[i] != 0; i++)
        {
            // replace space with underscore
            if (c[i] == 32)
            {
                c[i] = '_';
            }
        }
    }
}

Use

string text = "this is my text";
UnderscoreSpaces(text);
Console.WriteLine(text);
Console Output:
this_is_my_text
Send us feedback about this snippet »



Related Snippets: