Count a word inside a long string in c# [duplicate] - c#

This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Closed 8 years ago.
Can you please explain me how do i find in a simple way the number of times that a certain string occurs inside a big string?
Example:
string longString = "hello1 hello2 hello546 helloasdf";
The word "hello" is there four times. how can i get the number four.
Thanks
EDIT: I would like to know how i find a two word string as well for example:
string longString = "hello there hello2 hello4 helloas hello there";
I want to know the number of occurrences of "hello there".
EDIT 2: The regex method was the best for me (with the counts) but it does not find a word like this for example: ">word<" . Somehow, if I want to search for a word containing "<>" it skips it. Help?

just use string.Split() and count the results:
string wordToFind = "hello";
string longString = "hello1 hello2 hello546 helloasdf";
int occurences = longString
.Split(new []{wordToFind}, StringSplitOptions.None)
.Count() - 1;
//occurences = 4
to answer your edit, just change wordToFind to hello there

string longString = "hello1 hello2 hello546 helloasdf";
var regex = new Regex("hello");
var matches = regex.Matches(longString);
Console.WriteLine(matches.Count);

to do this with LINQ:
int count =
longString.Split(' ')
.Count(str => str
.Contains("hello", StringComparison.InvariantCultureIgnoreCase));
The only assumption here is that your longString is delimited by a space.

You can also use Linq to do this.
string longString = "hello1 hello2 hello546 helloasdf";
string target = "hello";
var count = longString.Split(' ').ToList<string>().Count(w => w.Contains(target));
Console.WriteLine(count);

Related

Get the rest of the string after occurrence of particular letters [duplicate]

This question already has answers here:
Regular Expression Groups in C#
(5 answers)
Closed 4 years ago.
string ABC = "This is test AZ12346";
Need value that occurs after AZ. AZ will always be present in the string and it will always be the last word. But number of characters after AZ will vary.
Output should be : AZ123456
Simply :
ABC.Substring(ABC.LastIndexOf("AZ"));
You can try LastIndexOf and Substring:
string ABC = "This is test AZ12346";
string delimiter = "AZ";
// "AZ123456"
string result = ABC.Substring(ABC.LastIndexOf(delimiter));
In case delimiter can be abscent
int p = ABC.LastIndexOf(delimiter);
string result = p >= 0
? ABC.Substring(p)
: result; // No delimiter found
If you are looking for whole word which starts from AZ (e.g. "AZ123", but not "123DCAZDE456" - AZ in the middle of the word) you can try regular expressions
var result = Regex
.Match(ABC, #"\bAZ[A-Za-z0-9]+\b", RegexOptions.RightToLeft)
.Value;

How to remove number character from string in c# [duplicate]

This question already has answers here:
How to remove the exact occurence of characters from a string?
(7 answers)
Closed 6 years ago.
i have a string like string st ="12,34,56,345,12,45" and i want remove number 34 from this string i did like string newst = st.replace(",34",""); but it is removing 34 from 345 , how to prevent this
EDIT
34 can be anywhere,here i am generating dynamically
It's very simple:
var st ="12,34,56,345,12,45";
var newst = st.replace(",34,", ",");
If it can be anywhere, you may use the regular expression:
var input = "34,234,35,36,34,37,345,34";
var pattern = #",?\b34\b,?";
var regex = new Regex(pattern);
var result = regex.Replace(input, ",").Trim(',');
Shorter notation could look like this:
var result = Regex.Replace(input, #",?\b34\b,?", ",").Trim(',');
Explanation of the regular expression: ,?\b34\b,? matches the word 34, but only if preceded and followed by word-delimiter characters (because of the word boundary metacharacter \b), and it can be (but doesn't have to be) preceded and followed by the comma thanks to ,? which means none or more comma(s).
At the end we need to remove possible commas from the beginning and end of the string, that's why there's Trim(',') on the result.
But I would say #crashmstr's solution is better than trying to tune the regular expression for this particular use case.
This will work:
var oldString = "34,12,34,56,345,12,45,34";
var newString = String.Join(",", oldString.Split(',').Where(x => x != "34"));
We split on ',', use LINQ to exclude "34", then join the string back together by ','.
Try this
string newst = st.replace(",34,",",");
Granted this only works if the number you want to replace is between two commas. If you want something more advanced, use Regex.Replace()
Here's an example:
string temp = Regex.Replace("12,34,56,345,12,45", #"^34,", "");
string newst = Regex.Replace(temp, #"34$,", "");
You could also use String.TrimStart and .TrimEnd to clean up the borders.
Also, I like crashmstr's example.
split and work in list:
string[] arr = st.Split(',');
List<string> list = arr.ToList();
list.Remove("34");
or regex:
var replaced = Regex.Replace(st, #"\b34\b[,]","");

How to search a word in string which consist of an English sentence? [duplicate]

This question already has answers here:
String compare C# - whole word match
(6 answers)
Closed 6 years ago.
Let's take an example of English sentence:
string sentence = "Hello, How are you?";
So if I want to search a word "you", I would use:
if (sentence.Contains("you")); // This would be true
But if I search this:
if (sentence.Contains("ello")); // This would also be true
But I want it to be False. I want to search the whole word. Not a part of word
How to do so in C#?
You can do this using regex.
var search = "ello";
string sentence = "Hello, How are you?";
string pattern = string.Format(#"\b{0}\b", search);
if(Regex.IsMatch(sentence, pattern))
{
}
You can split the sentence with Regex.Split() by word boundary and search for the word with simple Enumerable.Contains. It may be useful when you have to search for multiple words in one sentence (Just put them later into some HashSet for even better lookup):
var line = "Hello, How are you ? ";
var pattern = #"\b";
var words = new HashSet<String>(Regex.Split(line, pattern));
Console.WriteLine(words.Contains("Hello"));
Console.WriteLine(words.Contains("ello"));
Console.WriteLine(words.Contains("How"));
Or if you want to search for a single word once in a while, you can search directly by regex escaping the word and combining it with #"\b":
var line = "Hello, How are you ? ";
var wordBad = "ello";
var wordGood = "Hello";
Console.WriteLine(Regex.Match(line, $"\\b{Regex.Escape(wordBad)}\\b").Success);
Console.WriteLine(Regex.Match(line, $"\\b{Regex.Escape(wordGood)}\\b").Success);
You can use String.Split (combined with Select to remove punctuation) to get a list of words from the sentence, and then use Enumerable.Contains on that list:
var res = sentence.Split().Select(str => String.Join("", str.Where(c => Char.IsLetterOrDigit(c))))
.Contains("ello");
Console.WriteLine(res); // False

splitting the string and choosing the middle part containing two set of parenthesis [duplicate]

This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.

Extract number from end of string [duplicate]

This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 8 years ago.
I have some strings like "pan1", "pan2", and "pan20" etc. I need to extract number. I use it:
char ch = s[(s.Length) - 1];
int n = Convert.ToInt32(Char.GetNumericValue(ch));
But in case of, for example, "pan20" the result is not correct 0.
Index Approach
if you know where is the starting index of the number then simply you can do this :
string str = "pan20";
int number = Convert.ToInt32(str.Substring(3));
Note that "3" is the starting index of the number.
Fixed Prefix Approach
try to remove "pan" from the string; like this
string str = "pan20";
int number = Convert.ToInt32(str.Replace("pan", ""));
Regular Expression Approach
use regular expression only when string contains undetermined text inside
string str = "pan20";
int number = Convert.ToInt32(System.Text.RegularExpressions.Regex.Match(str, #"\d+").Value;
You can use for example regular expressions, for example [0-9]+$ to get the numbers in the end. See the Regex class in MSDN.

Categories

Resources