Click here to Skip to main content
12,701,828 members (36,077 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

0 views
Posted

Replace first occurence of a string in c#

, 4 Oct 2012 CPOL
And what is wrong with using string.Replace()?if (original==null) return string.Emptyelse return original.Replace(oldValue, newValue);Also, you are not reading file line by line, you read it whole at once into one string. If you have "This is actual sample" in 2 or more places, only...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

4 Oct 2012
DhanashreeRamya
2 alternatives  
The following is the code for replacing fist occurrence of a string in a file.public static string ReplaceFirstOccurrance(string original, string oldValue, string newValue){ if (String.IsNullOrEmpty(original)) return String.Empty; if (String.IsNullOrEmpty(oldValue)) ...
3 Oct 2012
psychic6000
because it deletes the position, insert() doesnt find any loctry this instead for your line return "original.Remove(loc, oldValue.Length).Insert(loc, newValue);"string temp = orignal.Substring(0, orignal.IndexOf(oldv));string temp2 = orignal.Substring(orignal.IndexOf(oldv) +...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sjelen
Software Developer (Senior) self employed
Serbia Serbia
No Biography provided

Pro
| | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 4 Oct 2012
Article Copyright 2012 by sjelen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid