请帮助我不能获得字符串的位置我的投入是“Dsakjfghap”我想知道“P”的位置 [英] Pls Help I Cant Get The Position Of A String My In Put Is "Dsakjfghap" I Want To Know The Position Of "P"
本文介绍了请帮助我不能获得字符串的位置我的投入是“Dsakjfghap”我想知道“P”的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace posi
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter string");
string s=Console.ReadLine();
string[]sa=new string[] {s};
for(int i=0;i<sa[i].Length-1;i++)
{
if(sa[i]=="p")
{
Console.WriteLine(i);
Console.ReadLine();
}
}
}
}
}
推荐答案
这里:
https://msdn.microsoft.com/en-us/library/system.string.indexof %28v = vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.string.lastindexof%28v=vs.110%29.aspx [ ^ ]。
-SA
Here:
https://msdn.microsoft.com/en-us/library/system.string.indexof%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.string.lastindexof%28v=vs.110%29.aspx[^].
—SA
你可以简单地使用IndexOf (),并使y我们的生活更加轻松
You can simply use IndexOf(), and make your life a hell of a lot easier
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace posi
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter string");
string s=Console.ReadLine();
int i = s.IndexOf('p');
Console.WriteLine(i);
Console.ReadLine();
}
}
}
好吧,首先,你试图将String分配给一个数组,然后你就完全错了。
你应该知道你可以索引一个字符串,就像索引一个数组一样。
例如,要得到i在下面的场景中,将它存储在变量c
Well, First off you are trying to assign the String to an array and you are going about that completely wrong.
You should know that you can index a String just like you can index an array.
For Example, To get "i" In the following scenario, and store it in variable c
String str = "Ridwan";
char c = str[1];
所以你的固定代码应该像这样(它有效)
So Your fixed code should be something Like this (it works)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace posi
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter string");
string s = Console.ReadLine();
for (int i = 0; i < s.Length - 1; i++)
{
if (s[i] == 'p')
{
Console.WriteLine(i);
Console.ReadLine();
}
}
}
}
}
这篇关于请帮助我不能获得字符串的位置我的投入是“Dsakjfghap”我想知道“P”的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文