-4
string text = "Today is a good day for help. **David Diaz He went to school.  **David Diaz like apple. ";

How to get how many times the text **David Diaz occurs in the string text?

UPDATED MY QUESTION

Peter Bons
  • 26,826
  • 4
  • 50
  • 74

3 Answers3

1

By using StartWhith you can check if the string starts whit ** if it is take the first two words of the string whits will represent the name

        string text = "**David Diaz He went to school.";

        if (text.StartsWith("**"))
        {
            var names = text.Split(' ')
                .Take(2)
                .ToArray();
            var fullName = names[0] + " " + names[1];
        }

UPDATE

As you said in the commend you want to look how many David Diaz occurs in one string, you can use regex for that.

  string text = "Today is a good day for help. **David Diaz He went to school. **David Diaz like apple. ";

        int matches = Regex.Matches(
            text,
            @"(?:\S+\s)?\S*David Diaz\S*(?:\s\S+)?",
            RegexOptions.IgnoreCase
        ).Count;
Timon Post
  • 2,779
  • 1
  • 17
  • 32
  • Thanks for code but I'm totally dumb. My actual text is string text = "Today is a good day for help. **David Diaz He went to school. **David Diaz like apple. "; In this code how to get how many **David Diaz have in the string text? – David Diaz Jun 03 '17 at 14:41
  • I've just did something like this. In this way(thanks to you) I can get arraylist with end of "bla bla **David Diaz" So in this list I need to get **David Diaz by split('****') ? var names = allText.Split('\t') .ToArray(); – David Diaz Jun 03 '17 at 14:54
  • See the update I've made this should work. You properly do'n know how to use regex but this is an way (just ignore all the weird symbols XD). – Timon Post Jun 03 '17 at 15:01
  • Thats's very awesome, I'm searching text dynamically. Is it possible to search in the text starts with ** and ends with \t and add them to string list? – David Diaz Jun 03 '17 at 15:25
  • Thanks mate, I've solved my problem half of your answer and half of marked answer. I don'T have a change to choose 2 answers, forgive me :) – David Diaz Jun 03 '17 at 15:44
0

Updated Answer: It sounds like you want to find the number of times a substring exists in your text. For that, you'll want to use RegEx.Matches, as explained in this answer: https://stackoverflow.com/a/3016577/682840

or LINQ, as explained in this answer: https://stackoverflow.com/a/541994/682840

Original Answer:

.StartsWith returns true/false if the string begins with the search string you provide. If you're wanting to know where a substring exists within your text, you'll need to use .IndexOf or a Regular Expression for more advanced scenarios.

IndexOf will return the location in the text where your provided search string starts (or -1 if it isn't found).

John M. Wright
  • 4,477
  • 1
  • 43
  • 61
  • Thanks, but I'm totally dumb. My actual text is string text = "Today is a good day for help. **David Diaz He went to school. **David Diaz like apple. "; In this code how to get how many **David Diaz have in the string text? – David Diaz Jun 03 '17 at 14:43
0
var text = "Today is a good day for help. **David Diaz He went to school. **David Diaz like apple. ";

var pos = 0;
var num = 0;
var search = "**David Diaz";
 while ((pos = text.IndexOf(search, pos)) > -1)
{
    num ++;
    pos += search.Length;
}

Console.WriteLine(num);

you can try out this in dotnetfiddle

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90
  • Thats's very awesome, I'm searching text dynamically. Is it possible to search in the text starts with ** and ends with \t and add them to string list? – David Diaz Jun 03 '17 at 15:25
  • @DavidDiaz That is achievable. But as per Stackoverflow rules, you need to ask one problem per question. create another question for other problems. don't forget to accept the answer which worked for you . It will be helpful for others to know which answer solved your problem. – Venkata Dorisala Jun 03 '17 at 15:38
  • Thanks mate, I've solved my problem half of your answer and half of Timon's answer. You guys rock! – David Diaz Jun 03 '17 at 15:44