Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...