c# - How to check if file contains strings, which will double repeat sign? -
i check if file containing strings, separated #
contains double repeat sign. example: have file this:
1234#224859#123567
i reading file , putting strings separated #
array. find strings have digit repeated next each other (in case 224859
) , return position of first digit repeats in string?
this have far:
arraylist list = new arraylist(); openfiledialog openfile1 = new openfiledialog(); int size = -1; dialogresult dr = openfile1.showdialog(); string file = openfile1.filename; try { string text = file.readalltext(file); size = text.length; string temp = ""; (int = 0; < text.length; i++) { if (text[i] != '#') { temp += text[i].tostring(); } else { list.add(temp); temp = ""; } } } catch (ioexception) { } string all_values = ""; foreach (object obj in list) { all_values += obj.tostring() + " => "; console.writeline(" => ", obj); } textbox1.text = (all_values);
this more procedural approach sriram's, main benefit remembering results in order use them later in program.
basically, string split based on #
delimiter, returns string[]
holds each number inside. then, each string iterate through characters , check see if current character @ i
matches next character @ i + 1
. if so, earliest appearance of duplicate digit @ i
, i
remembered , break out of loop processes char
s.
since int
non-nullable type, decided use -1
indicate match not found in string.
dictionary<string, int> results = new dictionary<string, int>(); string text = "1234#224859#123567#11#4322#43#155"; string[] list = text.split('#'); foreach (string s in list) { int tempresult = -1; (int = 0; < s.length - 1; i++) { if(s.elementat(i) == s.elementat(i + 1)) { tempresult = i; break; } } results.add(s, tempresult); } foreach (keyvaluepair<string, int> pair in results) { console.writeline(pair.key + ": " + pair.value); }
output:
1234: -1
224859: 0
123567: -1
11: 0
4322: 2
43: -1
155: 1
Comments
Post a Comment