查找字符串中字符的位置 [英] finding position of a character in a string
问题描述
大家好.
我有一个问题,我们如何才能找出字符在字符串值中的位置.
就像我在文本框中键入arun,然后在下一个文本框中键入"u".
然后单击buttn,它将在"arun"中显示"u"的位置.
谢谢您的宝贵建议,但仍然存在一个问题,
如果角色本身是重复的,wt会做,我们需要每次都找出它的位置.比如``ajay''中``a''的位置在索引0和2处.wt我应该找到两个position
hello guys.
i have a question, how can we find out position of a character in a string value.
like i type arun in a textbox, then i type ''u'' in next textbox.
then on the click of buttn it displays the position of ''u'' in ''arun''.
thanx for ur valuable suggestion ,but there is still a problum,
wt will we do if a character is repetation itself and we need to find out find out it position everytime.like position of ''a ''in ''ajay'' is at index 0 and 2 .wt should i do to find both positions
推荐答案
在按钮的click事件上,编写以下内容:
On the click event of the button, Write this:
String myName=TextBox1.Text;
String findCharacter=TextBox2.Text;
Int myCharacterIsAt=myName.IndexOf(findCharacter,1);
//It will return the index staring from zero.
// ie. in case of "u" you will get 2 as index
//Display the result
TextBox3.Text=myCharacterIsAt + 1; // Will return 3 in your example
由于问题已经改变,答案也将随之改变!
好吧,我会使用for循环.
Since the question changed, so will the answer!
Well i would be using a for loop.
int i=0;
String myName=TextBox1.Text;
String findCharacter=TextBox2.Text;
for(i=0;i<myname.length;i++)>
{
if(myName[i]==findCharacter)
{
TextBox3.Text=(i+1) + " ";
}
}
那是什么问题?
单击按钮后,获取textbox2中存在的文本,并使用它来搜索textbox1的文本.您可以根据需要使用各种属性之一.像IndexOf一样,等等.请在此处查看字符串的属性. [ ^ ]
So, what is the issue?
On click of button, get the text present in textbox2 and use it to search in text of textbox1. You can use one of various properties based on your need. Like IndexOf, etc. Have a look at the properties of string here.[^]
这篇关于查找字符串中字符的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!