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));
Related
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);
This question already has an answer here:
How can I substitute the value of one string into another based on a substitution character?
(1 answer)
Closed 3 years ago.
I have code that creates two strings:
var string1 = "ま|ちが|#";
var string2 = "間|違|う";
I'm looking for a way to combine these such the resulting output contains the characters from string1 but if the character is a "#" then it takes the alternate character from string2.
string1 string2 desired output
ま|ちが|# 間|違|う まちがう
な|# 為|る なる
で|き|# 出|来|る できる
Does anyone have any suggestions as to how I can do this?
You can use IndexOf() method like
var hashIndex = string1.IndexOf('#');
if(hashIndex > 0) {
Console.WriteLine(string1.Substring(0, string1.Length - 2) + string2[hashindex]);
}
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(".", "");
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
C# Splitting Strings on `#` character
(3 answers)
Closed 4 years ago.
I want to convert string to string array using "," separator. I'm using this code below but when string has an ampersand cuts the end of string.
string example = "one,two,three&four";
return new []{ example };
// result ["one,two,three"]
How can I get the result: ["one","two","three&four"] ?
You should use Split not Join.
Please try to this:
string[] result = example.Split(',');
This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 7 years ago.
Hi have a string which is: 2109389
I want to only keep the first two characters.
So my result will be 21.
var s = "2109389";
var sub = s.SubString(0,2);
var str = "2109389";
return str.SubString(0, 2);