Check if string has space in between (or anywhere) [duplicate] - c#

This question already has answers here:
Detecting whitespace in textbox
(4 answers)
Closed 4 years ago.
Is there a way to determine if a string has a space(s) in it?
sossjjs sskkk should return true, and sskskjsk should return false.
"sssss".Trim().Length does not seem to work.

How about:
myString.Any(x => Char.IsWhiteSpace(x))
Or if you like using the "method group" syntax:
myString.Any(Char.IsWhiteSpace)

If indeed the goal is to see if a string contains the actual space character (as described in the title), as opposed to any other sort of whitespace characters, you can use:
string s = "Hello There";
bool fHasSpace = s.Contains(" ");
If you're looking for ways to detect whitespace, there's several great options below.

It's also possible to use a regular expression to achieve this when you want to test for any whitespace character and not just a space.
var text = "sossjj ssskkk";
var regex = new Regex(#"\s");
regex.IsMatch(text); // true

Trim() will only remove leading or trailing spaces.
Try .Contains() to check if a string contains white space
"sossjjs sskkk".Contains(" ") // returns true

This functions should help you...
bool isThereSpace(String s){
return s.Contains(" ");
}

Related

How to delete diacritics from a string in C#? [duplicate]

This question already has answers here:
How do I remove diacritics (accents) from a string in .NET?
(22 answers)
Closed 9 months ago.
I have a string -
125DF885DF44é112846522FF001
I want to remove é from the string. When I search online I get solutions to remove the accents from é and returns e.
The diacritic character can come anywhere in the string and not in fixed place, also can be more than one.
How do I remove those?
You can use this
string s = "125DF885DF44é112846522FF001";
string s1 = s.Replace("é","");
In general case, we can remove symbols of unicode NonSpacingMark range:
We turn each symbol into pair: symbol + its mark(s) (that's the diacritics)
We remove marks
Combine symbols back
Code:
using System.Linq;
...
string source = "125DF885DF44é112846522FF001";
string result = string.Concat(source
.Normalize(NormalizationForm.FormD)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) !=
UnicodeCategory.NonSpacingMark))
.Normalize(NormalizationForm.FormC);

C#: get rid of multiple invalid characters in string [duplicate]

This question already has answers here:
Replace multiple characters in a C# string
(15 answers)
Closed 3 years ago.
I am new to C#. Say that I have a string like this:
string test = 'yes/, I~ know# there# are% invalid£ characters$ in& this* string^";
If I wanted to get rid of a single invalid symbol, I would do:
if (test.Contains('/'))
{
test = test.Replace("/","");
}
But is there a way I can use a list of symbols as argument of the Contains and Replace functions, instead of deleting symbols one by one?
I would go with the regular expression solution
string test = Regex.Replace(test, #"\/|~|#|#|%|£|\$|&|\*|\^", "");
Add a | or parameter for each character and use the replace
Bear in mind the \/ means / but you need to escape the character.
You'll likely be better off defining acceptable characters than trying to think of and code for everything you need to eliminate.
Because you mention that you are learning, sounds like the perfect time to learn about Regular Expressions. Here are a couple of links to get you started:
Regular Expression Language - Quick Reference (MSDN)
C# Regex.Match Examples (DotNetPerls
I don't think there is such a feature out of the box.
I think your idea is pretty much on point, despite the fact the in my opinion you don't really need the if(test.Contains(..)) part. Doing this, once you iterate the characters of the string to see if such element is present when at the end if indeed this character is in the string you replace it
It would be faster just to replace the special characters right away. So...
List<string> specialChars = new List<string>() {"*", "/", "&"}
for (var i = 0; i < specialChars.Count; i++)
{
test = test.Replace(specialChars[i],"");
}
Your solution is:
Path.GetInvalidPathChars()
So the code would look something like this:
string illegal = "yes/, I~ know# there# are% invalid£ characters$ in& this* string^";
string invalid = new string(Path.GetInvalidFileNameChars()) + new
string(Path.GetInvalidPathChars());
foreach (char c in invalid)
{
illegal = illegal.Replace(c.ToString(), "");
}
Another variant:
List<string> chars = new List<string> {"!", "#"};
string test = "My funny! string#";
foreach (var c in chars)
{
test = test.Replace(c,"");
}
No need to use Contains as Replace does that.

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.

Remove characters between different parameters [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I have a string of different emails
ex: "email1#uy.com, email2#iu.it, email3#uu.edu" etc, etc
I would like to formulate a Regex that creates the following output
ex: "email1,email2,email3" etc, etc
How can I remove characters between an "#" and "," but leaving a "," and a Space in C#
Thank you so much for the help!!
If you want to replace all characters between # and comma by blank, the easiest option is to use Regex.Replace:
var emails = "a#m.com, b#m.com, d#m.com";
var result = Regex.Replace(emails, "#[^,]+", string.Empty);
// result is "a, b, d"
Please note that it leaves spaces after comma in the result, as you wanted in your question, though your example result has spaces removed.
The regular expression looks for all substrings starting '#' characters, followed by any character which is not comma. Those substrings are replaced with empty string.
Replacing all occurrences of #[^,]+ with an empty string will do the job.
The expression matches sequences that start in #, inclusive, up to a comma or to the end, exclusive. Therefore, commas in the original string of e-mails would be kept.
Demo.
Maybe you don't need to use a regex, in that case you can do the following:
string input = "email1#uy.com, email2#iu.it, email3#uu.edu";
input = input.Replace(" ", "");
string[] ocurrences = input.Split(',');
for (int i = 0; i < ocurrences.Length; i++)
{
string s = ocurrences[i];
ocurrences[i] = s.Substring(0, s.IndexOf('#'));
}
string final = string.Join(", ", occurences);

Regex Limit the count of characters in a string [duplicate]

This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Closed 9 years ago.
Here is my string
string countCommas = 12,34,56
I am looking for REGEX for algorithm below
BOOL isCountExaclty2 = if(number of commas in string == 2){return TRUE;}else return FALSE
I want the right hand expression as one single REGEX expression which returns either TRUE or FALSE but not the count
(I know to use Regex.COUNT..but it ends up in 2 statements)
If you're looking for a pattern that will only match if there's exactly two commas in the string, this should work:
bool isCountExactly2 = Regex.IsMatch("12,34,56", "^([^,]*,){2}[^,]*$");
But regular expressions really aren't the right tool for this job.
Try this :
string countCommas = "12,34,56"
bool isCountExaclty2 = Regex.Split(countCommas, ",").Length == 2;

Categories

Resources