How to split two strings and swap sliced parts in C#? -
i have array , each index contains string 4 characters. need select random point in string , slice stringaarray[0] , stringaarray[1] @ same point , swap sliced parts , add these splicedstringarray[0] , splicedstringarray[1].
i know how use split in c# , have been experimenting this, split string characters, not parts. ask question because way of thinking create lots of variables hold temporary strings add them splicedstringarray[].
here latest attempt find start middle , end of string , copy whatever want variables make new strings , store these in teh second array:
string s = stringaarray[0]; char[] chararray = s.tochararray(); int amount = chararray.length; int findmiddle = amount / 2 + 1; int midchar = findmiddle - 1; int findlast = amount - 1; char fchar = chararray[0]; char mchar = chararray[midchar]; char lchar = chararray[findlast]; i looking @ string builder class in c# , wondering if there there use, think spend lot of time on , and develop worst solution advice on how appreciated.
for splitting @ exact position, use string.substring. way can split up to point , from point. simplest solution similar this:
var offset = 1; splicedstringarray[0] = stringarray[0].substring(0, offset) + stringarray[1].substring(offset); splicedstringarray[1] = stringarray[1].substring(0, offset) + stringarray[0].substring(offset); disclaimer: code written without testing.
Comments
Post a Comment