C# String Replace Extension

This is a discussion on C# String Replace Extension within the C# Programming forums, part of the General Programming Boards category; Script; Code: public static class Extension { public static string Replace ( this string subject, string search, string replace, int ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    C# String Replace Extension

    Script;
    Code:
    public static class Extension {
            public static string Replace(this string subject,string search, string replace, int count) {
                string new_String = "";
                for(int i = 0; i < subject.Length; i++){
                    if(subject[i] == search[0]) {
                        string che = "";
                        for(int e = 0; e < search.Length; e++) {
                            if (subject[i+e] == search[e]) {
                                che += search[e];
                            }
                        }
                        if (che == search) {
                            if (count > 0) {
                                new_String += replace;
                                i += search.Length-1;
                                count--;
                            }
                            else {
                                new_String += subject[i];
                            }
                        }
                        else {
                            new_String += subject[i];
                            
                        }
                    }
                    else {
                        new_String += subject[i];
                    }
                }
                return new_String;
            }
            public static string Replace(this string subject,char search, char replace, int count) {
                string new_String = "";
                for(int i = 0; i < subject.Length; i++){
                    if(subject[i] == search) {
                        if (count > 0) {
                            new_String += replace;
                            count--;
                        }
                        else {
                            new_String += subject[i];
                        }
                    }
                    else {
                        new_String += subject[i];
                        
                    }
                }
                return new_String;
            }
        }
    Example1:
    Usage:
    Code:
    "Hey Hey".Replace("He", "he", 1);
    Result:
    Code:
    hey Hey

    Example2:
    Usage:
    Code:
    "Heystack HeyStack".Replace('e', 'a', 1);
    Result:
    Code:
    Haystack HeyStack

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                String str1 = "abc abc";
                String str2 = str1.Replace("b", "d", 1);
                Console.WriteLine(str2); // adc abc
                Console.Read();
            }
        }
    
        static class StringExtension
        {
            public static String Replace(this String str, String oldStr, String newStr, int limit)
            {
                return new Regex(Regex.Escape(oldStr)).Replace(str, newStr, limit);
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,407
    do you have a question? is there something wrong with the code you posted? does it do what you want? if not, what is it? does it give you compiler errors?

    you have given absolutely no context to your post, and nobody here will have any idea how to help you, if in fact you need help.
    Code:
    namespace life
    {
        const bool change = true;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String replace
    By guitarist809 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2008, 03:53 PM
  2. String Replace!
    By Moni in forum C Programming
    Replies: 2
    Last Post: 09-04-2006, 10:40 PM
  3. I found a replace string by string...how secure it is?
    By Joelito in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2006, 12:47 AM
  4. Trying to replace % with %% in a string
    By willz99ta in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 10:50 AM
  5. Replace elements in string
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2005, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21