Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

c# - How to check if string contains single occurence of substring?

I have this:

string strings = "a b c d d e";

And I need something similar to string.Contains(), but I need to know not only whether a string is present(in case above a letter), but also if it is present only ONE SINGLE time.

How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use LastIndexOf(String) and IndexOf(String) and verify that the values returned are equal. Of course also check if the String is found at all(i.e the returned value is not -1).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...