How to replace space/character with specified character after first numeric value in string using C#? -
i have string
containing special characters & numeric values
.
eg: 3-3 3 3-3"3/4 3-3-3/4 3-3 3/4 3 3 3 3'3 3 output must be: 3f3 3 3f3"3/4 3f3-3/4 3f3 3/4 3f3 3 3f3 3
i have tried using :
public static string replacefirst(string text, string search, string replace) { int pos = text.indexof(search); if (pos < 0) { return text; } return text.substring(0, pos) + replace + text.substring(pos + search.length); }
using above code replaces first occurrence specified character works fine.
eg: 3-3 3 output: 3f3 3
but 3 3-3 output: 3 3f3
according code correct. want replace space/character after first numeric 3f3-3
help appreciated!
simple loops should work.
for(int =0; < mylistofstrings.count; i++) { char[] chars = mylistofstrings[i].tochararray(); (int j = 0; j < chars.count(); j++) { if (char.isdigit(chars[j])) { if(j + 1 < chars.count()) { chars[j + 1] = 'f'; //'f' being replacing character mylistofstrings[i] = new string(chars); } break; } } }
output examples
mylistofstrings.add("3-3 3"); mylistofstrings.add("3-3-3/4"); mylistofstrings.add("3 3-3"); mylistofstrings.add("3 3 3"); 3f3 3 3f3-3/4 3f3-3 3f3 3
update comments
for (int = 0; < mylistofstrings.count; i++) { char[] chars = mylistofstrings[i].tochararray(); (int j = 0; j < chars.count(); j++) { if (!char.isdigit(chars[j])) { if (j + 1 < chars.count()) { chars[j] = 'f'; //'f' being replacing character mylistofstrings[i] = new string(chars); } break; } } }
Comments
Post a Comment