C# 中字符串中存在的索引的位置
本文关键字:索引 位置 存在 字符串 | 更新日期: 2023-09-27 18:34:08
基本上,我正在制作一个包含两部分
的程序1(将给定的字符串从十六进制转换为二进制(做到了(
我遇到问题的部分是:-
2(打印转换字符串中1的位置例如,我转换后的字符串是
1011000001001 所以我想打印 1,3,4,10,13(这些是字符串中 1 的位置(
我的代码是:-
private void button1_Click(Object sender, EventArgs e(
{
string temp = textBox1.Text;
string binary = ConvertTOBinary(temp);
//Console.WriteLine(binaryval);
}
公共字符串转换到(字符串温度( {
string binaryval = "";
binaryval = Convert.ToString(Convert.ToInt64(temp, 16), 2);
MessageBox.Show(binaryval);
var indexes = binaryval
.Select((c, index) => c == '1' ? index + 1 : 0)
.Where(indexPlus1 => indexPlus1 > 0);
var indexesText = string.Join(",", indexes);
MessageBox.Show(indexes);
return binaryval;
}
我收到错误:-
1 "字符串"的最佳重载方法匹配。Join(string, string[](' 有一些无效的参数'
2':无法从"System.Collections.Generic.IEnumerable"转换为"string[]"
3 'System.Windows.Forms.MessageBox.Show(string(' 的最佳重载方法匹配有一些无效的参数 C:''Documents and Settings''Hamza''My Documents''Visual Studio 2008''Projects''Import-Compare''Import-Compare''Parser.cs 46 13 导入比较错误
4 参数"1":无法从"System.Collections.Generic.IEnumerable"转换为"字符串">
请帮助需要! :(
Linq 是一件很棒的事情,但有时以"ole school"的方式做事更容易,更重要的是更清晰:
var test = "1011000001001";
var answer = new StringBuilder();
var ix = test.IndexOf('1');
while (ix > -1) {
ix = ix + 1;
answer.AppendFormat("{0},", ix);
ix = test.IndexOf('1', ix);
}
var counts = answer.ToString().TrimEnd(new char[] { ',' });
Console.WriteLine(counts);
请记住,有一天您可能必须返回该代码并弄清楚它的作用。