我可以在C#的indexof和index中使用2个字符吗? [英] Can I use 2 characters in indexof and index in C#?

查看:223
本文介绍了我可以在C#的indexof和index中使用2个字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Indexof(',',int32,int32,stringcomparison)



我有一个自动提取并显示列表框的自定义文本框。我希望它能在每个逗号后用空格','不仅仅是空格或逗号。提前致谢。我是c#btw的新手。我正在使用winforms。



我尝试过:



目前还没有。我仍在为此重新研究材料和知识。

Indexof(', ', int32, int32, stringcomparison)

I have a customized textbox that autosuggests and shows listbox. I want it to suggest every after a comma with space ', ' not just a space or a comma. Thanks in advance. I'm new to c# btw. Im using winforms.

What I have tried:

None yet. I'm still reasearching for materials and knowledge for this.

推荐答案

是: String.IndexOf Method(String)(System) [ ^ ]

Yes: String.IndexOf Method (String) (System)[^]
int index = myString.IndexOf(", ");


查看 String.IndexOf方法(系统) [ ^ ]重载。官方文档始终是第一个在不确定时阅读的文档。它通常还包含一些相关功能列表,这些功能可能会在最初的功能没有时执行您想要的功能。



如果要搜索,,请使用一个接受字符串的 IndexOf 方法:

See the list of the String.IndexOf Method (System)[^] overloads. The official documentation is always the first to be read when being unsure. It contains also often a list of related functions that might do what you want when the initial one does not.

If you want to search for ", ", use one of the IndexOf methods accepting a string:
Quote:

报告当前String对象中第一次出现指定字符串的从零开始的索引。

Reports the zero-based index of the first occurrence of the specified string in the current String object.

另请注意,不必使用接受 comparisonType 对于仅包含ASCII字符的搜索字符串。



如果要搜索更复杂的案例(如逗号可选,后跟一个或多个空格),看一下正则表达式。但是,如果可以使用此处的基本功能完成任务,请使用该功能。

Note also that it is not necessary to use an overload accepting a comparisonType for a search string containing only ASCII characters.

If you want to search for more complex cases (like a comma optionally followed by one or more spaces), have a look at regular expressions. But when a task can be done with a basic function like here, use that.


这篇关于我可以在C#的indexof和index中使用2个字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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