Yesterday, my friend interviewed for Android, pen questions. Completely do not test Android, test programming basis (PS: Also completely handwritten on paper)
The following: Implementing the Split function
Requirements
Function one: realize single character cutting. such as: Split (' | ');
Requirement two: To achieve the cut of the string, such as: Split ("abc");
So I gave the first version of
Public Static string[] Spiltstrings ( This stringStrCharsplitstr) { if(!Str. EndsWith (Splitstr.tostring ())) {str+=Splitstr; } varList =Newlist<string>(); vartemp =0; for(inti =0; I < Str. Length; i++) { varitem =Str[i]; if(string. Equals (item, SPLITSTR)) {list. ADD (str. Substring (temp, I-temp)); Temp=Str. IndexOf (item, i); STR= str. Remove (I,1); } } returnlist. ToArray (); }
Call Method:
var str = "123|45| | "; var a = str. Spiltstrings (' | ');
The results of the operation are as follows:
The result of the self-spiit function is as follows
There is still a certain gap.
The Continuous "| |" cannot be resolved temporarily. Problem
Summary of the problems encountered:
One: foreach (var item in str): Take Str.indexof (item); Item may have more than one string. IndexOf (item) is always the first one unable to locate his correct position (PS: Available indexOf (Char,startindex), Trouble)
Two: Mainly rely on the substring function, easy to cause the detail problem. Difficult to handle
Three: Str. Remove (index); Remember to reassign: str = str. Remove (index);
Upgrade version
Public Static string[] spiltstrings_s ( This stringStrCharsplitstr) { varList =Newlist<string>(); varSB =NewStringBuilder (); if(!Str. EndsWith (Splitstr.tostring ())) {str+=Splitstr; } foreach(varIteminchstr) {sb. Append (""); if(!string. Equals (item, SPLITSTR)) {sb. Append (item); } Else{list. ADD (sb.) ToString ()); SB=NewStringBuilder (); } } returnlist. ToArray (); }
The results of the operation are as follows:
Okay, get it done.
It seems simple, but we are all standing on the shoulders of giants.
Function Two: There is no way to find what you can do, temporarily paste this. Changed on the original version.
The code is as follows:
Public Static string[] Spiltstrings ( This stringStrstringsplitstr) { varList =Newlist<string>(); vartemp =0; varSplitlenth =splitstr.length; varEndstr = str. Substring (str. Length-splitlenth); if(!string. Equals (Endstr, splitstr)) {str+=Splitstr; } for(vari =0; I < Str. Length; i++) { varSTR1 =string. Empty; for(varj =0; J < Splitlenth; J + +) { if(i + J >=Str. Length) { Break; } str1+ = Str[i +J]; } if(string. Equals (STR1, splitstr)) {list. ADD (str. Substring (temp, I-temp)); Temp=i; STR=Str. Remove (i, splitstr.length); } } returnlist. ToArray (); }
Call:
var " 12454$$454$$$4445 " ; var b = str1. Spiltstrings ("$$");
Operation Result:
There are also problems:
One: When multiple consecutive occurrences of "$$$", cutting symbol "$$", the demand for cutting is not clear
----------------------------------------------------------------------------------------
Finally, welcome to the big God paste algorithm solution, the point is insufficient. I'm a rookie, need to learn
. NET implementation split character cutting