Non-Blocking Read from Standard I/O in C#

Get only Whole Words from a .Contains() statement

You could split your sentence into words - you could split at each space and then trim any punctuation. Then check if any of these words are 'hi':

var punctuation = source.Where(Char.IsPunctuation).Distinct().ToArray();
var words = sentence.Split().Select(x => x.Trim(punctuation));
var containsHi = words.Contains("hi", StringComparer.OrdinalIgnoreCase);

See a working demo here: https://dotnetfiddle.net/AomXWx

string.contains(string) match whole word

It makes sense that the search would fail, since it's not an exact match.

Try using StartsWith instead, to see if any key starts with the value you're looking for.

if (!form.AllKeys.Any(x => x.StartsWith("anotherText")))
{
// add error
}

string.IndexOf search for whole word match

You can use Regex

string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
var indx = Regex.Match(str, @"\WTOTAL\W").Index; // will be 18

How to extract a whole word from a sentence by a specific fragment in C#?

You can use a regular expression to solve this. Therefore you can use a simple approach: Take each word containing .0. inside. Each word should be surrounded by a space. The expression which results could look like this:

Visualized regex

Here is an implemented sample of the expression:

string input = "The app has been updated to 88.0.1234.141 which contains a number of fixes and improvements.";
MatchCollection matches = Regex.Matches(input, @"[^-\s]*\.0\.[^-\s]*");
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}

The output will be

88.0.1234.141

Way to have String.Replace only hit whole words

A regex is the easiest approach:

string input = "test, and test but not testing.  But yes to test";
string pattern = @"\btest\b";
string replace = "text";
string result = Regex.Replace(input, pattern, replace);
Console.WriteLine(result);

The important part of the pattern is the \b metacharacter, which matches on word boundaries. If you need it to be case-insensitive use RegexOptions.IgnoreCase:

Regex.Replace(input, pattern, replace, RegexOptions.IgnoreCase);

How do I exactly match a word with C#'s contains function?

How about a regex?

bool contains = Regex.IsMatch(m.text, @"\bMaterial\b");

Regex matching the entire string and nothing else

You probably forgot to put ^ at the start of the pattern and $ at the end of it..

..but if you're searching for an exact string, just use == or .Equals - regex is an unnecessary performance hit.

If you mean that you have a document full of words and you want "whole word" style searching, use \b before and after the pattern:

var input = "This is a test that is testing latest regex attempts";
var r = new Regex(@"\btest\b");
r.IsMatch(input); //true

input = "Now I attest we're testing non whole word tests";
r.IsMatch(input); //false

The \b means "word boundary" and is conceptually things like "start of input", "whitespace", "punctuation" etc so putting at the start and end of "test" means that if the document contains "test" it matches but it doesn't match "attest" or "testing"



Related Topics



Leave a reply



Submit