打印的&QUOT号码的位置; 1的"目前在C#中的字符串 [英] Print the Position of Number of "1's" present in a string in C#

查看:136
本文介绍了打印的&QUOT号码的位置; 1的"目前在C#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个方案,一个十六进制值二进制值转换。还有我挺了过来,但现在我在我的计划的第二部分是



现在我想显示1,目前的位置面临着一个问题,我二进制文件,是由
类似
例如
我的二进制文件




0100100101101




应该告诉我




2,5,8, 10,11,13




 私人无效的button1_Click(对象发件人,EventArgs五)

{
字符串TEMP = textBox1.Text;
串二进制= ConvertTOBinary(TEMP);


} //这是我的按钮

公共字符串ConvertTOBinary(字符串TEMP)
{

的代码字符串binaryval =;
binaryval = Convert.ToString(Convert.ToInt64(温度,16),2);
MessageBox.Show(binaryval);
返回binaryval;
}



我在做这在C#中!


解决方案

  VAR指标= binary.Select((X,I)=>新建{位数= X,索引= I})
。凡(X => x.digit =='1')
。选择(X => x.index + 1);

或正则表达式

  VAR指标= Regex.Matches(二进制1)
.Cast<匹配和GT;()
。选择(X => x.Index + 1);

 列表与LT; INT>指数=新的List< INT>(); 
的for(int i = 0; I< binary.length;我++)
{
如果(二进制[I] =='1')indexes.Add(I + 1);
}


I am making a program That Converts a hexa value to a binary value . well i did make it but now i am facing an issue in the 2nd part of my program which is

Now i want to display the position of "1" present in my Binary Which is made Like For example my binary is

0100100101101

it should tell me

2,5,8,10,11,13

private void button1_Click(object sender, EventArgs e)

     {
        string temp = textBox1.Text;
        string binary = ConvertTOBinary(temp);


     }                  // This is the code of my Button 

public string ConvertTOBinary(string temp)
    {

        string binaryval = "";
        binaryval = Convert.ToString(Convert.ToInt64(temp, 16), 2);
        MessageBox.Show(binaryval);
        return binaryval;
     }

I am making it in C# !!

解决方案

var indexes=binary.Select((x,i)=>new{digit=x,index=i})
                  .Where(x=>x.digit=='1')
                  .Select(x=>x.index+1);

or in regex

var indexes=Regex.Matches(binary,"1")
                 .Cast<Match>()
                 .Select(x=>x.Index+1);

or

List<int> indexes=new List<int>();
for(int i=0;i<binary.length;i++)
{
    if(binary[i]=='1')indexes.Add(i+1);
}

这篇关于打印的&QUOT号码的位置; 1的&QUOT;目前在C#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆