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

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);

Related

How to find the immediate integer value written before a string? [duplicate]

This question already has answers here:
How to get the digits before some particular word using regex in c#?
(8 answers)
Closed 2 years ago.
How to find the immediate integer value written before a string in c#? For example
50+ boxes were ordered, however only 2 are delivered.
I need to know the number of boxes (integer value) written just before "delivered". The output should be 2. I have written a code in c# using Regex:
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"\d+").Value;
//The output I get is 50 instead of 2.
To get the last number that is followed by the word "delivered", you may use the following pattern:
\b\d+\b(?=[^\d]*\bdelivered\b)
Regex demo.
Here's a full example:
string line = "50+ boxes were ordered, however only 2 are delivered.";
var match = Regex.Match(line, #"\b\d+\b(?=[^\d]*\bdelivered\b)");
if (match.Success)
{
string boxesDelivered = match.Value;
// TODO: convert the value to a numeric type or use it as is.
}
Try it online.
written just before delivered
I'm going to take that verbatim as your user requirement - find the last number in the string that appears before "delivered".
You can use (\d+)[^\d]*(?:delivered), which says "match any sequence of numbers that does not occur before another sequence of numbers and does occur before delivered".
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"(\d+)[^\d]*(?:delivered)").Groups[1].Value;
// boxesDelivered = 2

Trim is not returning a string without the characters I want to remove [duplicate]

This question already has answers here:
C# Trim() vs replace()
(8 answers)
Closed 3 years ago.
I want to remove 2 chars from a string: '-', '.'
According to microsoft documentation, if I provide a array of characters as a parameter to trim, it should remove then and return a new string.
Here is the code I trying:
char[] charsToTrim = { '-', '.'};
string newCPF = usuario.Cpf.Trim(charsToTrim);
usuario.Cpf = newCPF;
_context.Add(usuario);
The string is something like this 000.000.000-00, but trim is not removing the . and -
What I'm doing wrong?
Trim only removes the specified characters from the start and the end of the string.
You could just use String.Replace to remove those characters:
string newCPF = usuario.Cpf.Replace("-", "").Replace(".", "");

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.

Removing characters if it appears more than once [duplicate]

This question already has answers here:
How do you remove repeated characters in a string
(7 answers)
Closed 10 years ago.
I have a string given by user. After the user entry i want the character '-' to appear only once even if appears twice or more.
DF--JKIL-L should be DF-JKIL-L
`DF-----JK-L-` should be `DF-JK-L-`
A simple regular expression should do the trick:
string originalString = "DF-----JK-L-";
string replacedString = Regex.Replace(originalString, "-+", "-");
You can use Split with option StringSplitOptions.RemoveEmptyEntries, then Join again:
var result = string.Join("-",
input.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries));

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

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(" ");
}

Categories

Resources