Delete \ from string C# [duplicate] - c#

This question already has answers here:
Remove characters from C# string
(22 answers)
Closed 1 year ago.
I need to delete \, ", [ and ] from this string:
"[\"30001|410000000|400000000|PocketPC_Login|1\",\"30002|420000000|400000000|PocketPC_ChangementZone|1\",\"30003|430000000|400000000|PocketPC_Transactions|1\",\"30004|440000000|400000000|PocketPC_Gestion|1\",\"30005|450000000|400000000|PocketPC_Administration|0\",\"30006|431000000|430000000|PocketPC_TSEntrees|1\"]"
When I do this:
string.Trim(new Char[] { '"', '[', ']', "\\"})
or this:
string.Trim(new Char[] { '"', '[', ']', Convert.ToChar(92)})
the result is always the same:
"30001|410000000|400000000|PocketPC_Login|1\",\"30002|420000000|400000000|PocketPC_ChangementZone|1\",\"30003|430000000|400000000|PocketPC_Transactions|1\",\"30004|440000000|400000000|PocketPC_Gestion|1\",\"30005|450000000|400000000|PocketPC_Administration|0\",\"30006|431000000|430000000|PocketPC_TSEntrees|1"
The \ and " still there. What could I do?

Trim only removes items at the start or the end of a string. You should use a chain of string.Replace.
myString
.Replace("\\", String.Empty)
.Replace("\"", String.Empty)
.Replace("[", String.Empty)
.Replace("]", String.Empty);
Something else I just noticed, is that your data is likely to be JSON (the square brackets [] suggest that you have an array of strings).

Related

c# how to append arrays without duplicates [duplicate]

This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Closed 19 days ago.
The below example splits two strings of codes and appends them together. The problem is it doesn't filter duplicates. In this case allCodes has "DL" twice. Is there a way to do the split & concatenate without dups?
string carCodes = "DL, UL, AL, ";
string airCodes = "RL, DL";
string[] allCodes = airCodes
.Replace(" ", "")
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Concat(carCodes
.Replace(" ", "")
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
A distinct does it.
.Distinct().ToArray();

How do I find all start indexes of word in string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a string where all words separated with these chars:
{ ' ', '.', ',', '!', '?', ':', '-','\r','\n' };
And words can be separated MORE then one separator;
I tried replace all separators on spaces and add space to start and end of text and in loop find
IndexOf(" "+word+" ",i+word.Length)
but i think exists more faster ways to make it
You can search for words using regex without having to specify all possible separators.
string input = "This, is? a test-word!\r\nanother line.";
var matches = Regex.Matches(input, #"\w+");
foreach (Match m in matches) {
Console.WriteLine($"\"{m.Value}\" at {m.Index}, length {m.Length}");
}
prints:
"This" at 0, length 4
"is" at 6, length 2
"a" at 10, length 1
"test" at 12, length 4
"word" at 17, length 4
"another" at 24, length 7
"line" at 32, length 4
The expression \w+ specifies a sequence of one or more word characters. This includes letters, digits and the underscore. See Word Character: \w for a detailed description of \w.
You can replace all (possibly multiple) separators by spaces like this:
char[] separators = new char[] { ' ', '.', ',', '!', '?', ':', '-', '\r', '\n' };
var words = input.Split(separators, StringSplitOptions.RemoveEmptyEntries);
string result = String.Join(" ", words);
Console.WriteLine(result);
prints
This is a test word another line
The StringSplitOptions.RemoveEmptyEntries parameter ensures that sequences of multiple separators are treated like one separator.

How to sanitize a string removing specific character? [duplicate]

This question already has answers here:
Is there a simple way to remove multiple spaces in a string?
(26 answers)
How do I replace multiple spaces with a single space in C#?
(28 answers)
Closed 4 years ago.
I have this string:
PO Box 162, Ferny Hills
QLD 4055
Brisbane
which contains these character:
I want remove this charactes, so I tried:
info.Address = dd[i].InnerText
.Replace("\n", " ")
.Replace(" ", "")
.Replace(",", ", ");
but didn't works, I get all the character of the string attached. I'm expecting this result: PO Box 162, Ferny Hills QLD 4055 Brisbane.
Well, you replaced all blanks by nothing ( .Replace(" ", "") ). What did you expect? Now all your blanks are gone.
If you don't want that, don't do it.
you can try like this, by splitting and trimming the string to remove spaces and join them back by newline
var address = dd[i].InnerText.Split(new[] { Environment.NewLine })
.Select(s => s.Trim());
info.Address = String.Join(Environment.NewLine, address);

How to split a string by multiple chars? [duplicate]

This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 6 years ago.
I have a string like this: string ip = "192.168.10.30 | SomeName".
I want to split it by the | (including the spaces. With this code it is not possible unfortunately:
string[] address = ip.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
as this leads to "192.168.10.30 ". I know I can add .Trim() to address[0] but is that really the right approach?
Simply adding the spaces(' | ') to the search pattern gives me an
Unrecognized escape sequence
You can split by string, not by character:
var result = ip.Split(new string[] {" | "}, StringSplitOptions.RemoveEmptyEntries);
The Split method accepts character array, so you can specify the second character as well in that array. Since you ware used RemoveEmptyEntries those spaces will be removed from the final result.
Use like this :
string[] address = ip.Split(new char[] { '|',' '}, StringSplitOptions.RemoveEmptyEntries);
You will get two items in the array
"192.168.10.30" and SomeName
This might do the trick for you
string[] address = ip.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();

Split by two or more spaces [duplicate]

This question already has answers here:
Split string separated by multiple spaces, ignoring single spaces
(3 answers)
Closed 7 years ago.
Lets say I have a string like this:
"15 Lore ipsum"
Is there a way to split it so I got 3 strings after split ["15", "Lore", "Ipsum"]. So I want delimiter to be a space consisting of two or more spaces, I don't want one space to be delimiter...
You can use string.Split(char[], StringSplitOptions) method:
string s = "15 Lore ipsum";
string result = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
\s stands for "whitespace character".
http://www.w3schools.com/jsref/jsref_regexp_whitespace.asp
Try (.*?)\s+(.*?)\s+(.*?)$
https://regex101.com/r/oH8qY6/1

Categories