Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

c# - Split on every fourth comma in a string and count

I have a string like this: 1,b,1,28,2,g,9,95,3,y,4,60,4,r,4,8,5,b,10,46,6,b,10,45,7,g,8,94,8,r,8,16,9,y,3,58,15,r,10,20,16,g,5,87,19,r,1,2,21,y,1,54,22,b,3,31,1,r,9,17

I want to split this every fourth time I hit a comma and afterwards count how many times it has been split, but I have no idea how to do so :-/

I came across this, but it is JavaScript and I want to be able to do this in C#:

var j:int = 0;
var h:String;
for(var thisvalue:String in thestring){
    h += (j%4 == 3)?thisvalue+';':thisvalue+','; 
    j++;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
var cnt = (thestring.Count(x => x == ',') + 1) / 4;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...