一个字符串:str="选项一|2、选项二|"怎样取这个字符串中“|”的个数??- --------------------------------------你的意思是计算出‘|’的个数? int num = 0; for(int i = 0 ; i < str.Length; i++) { if(str[i]=='|') { num ++; } } num就是计算得到的个数。
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace CSDNTestApp { class Program { static void Main(string[] args) { string targetString = "选项一|2、选项二|"; var result = from target in targetString where target == '|' select target; int count = result.Count(); Console.WriteLine("|的个数为: " + count); Console.ReadLine(); } } } 也可以用LINQ~
第一种方法: string str="选项一|2、选项二|"; string str2 = str.Replace("|", ""); int count = str2.Length - str.Length;第二种方法: int num = 0; for(int i = 0 ; i < str.Length; i++) { if(str[i]=='|') { num ++; } }第三种方法: string[] strarr=str.split('|'); return strarr.length-1;
string str2 = str.Replace("|", "");
int count = str2.Length - str.Length;
--------------------------------------你的意思是计算出‘|’的个数?
int num = 0;
for(int i = 0 ; i < str.Length; i++)
{
if(str[i]=='|')
{
num ++;
}
}
num就是计算得到的个数。
return strarr.length-1;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace CSDNTestApp
{
class Program
{
static void Main(string[] args)
{
string targetString = "选项一|2、选项二|";
var result = from target in targetString
where target == '|'
select target;
int count = result.Count();
Console.WriteLine("|的个数为: " + count);
Console.ReadLine();
}
}
}
也可以用LINQ~
string str="选项一|2、选项二|";
string str2 = str.Replace("|", "");
int count = str2.Length - str.Length;第二种方法:
int num = 0;
for(int i = 0 ; i < str.Length; i++)
{
if(str[i]=='|')
{
num ++;
}
}第三种方法:
string[] strarr=str.split('|');
return strarr.length-1;