Create string[] from string with ampersand [duplicate] - c#

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(',');

Related

How can I create a string by combining two strings and using substitution? [duplicate]

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

Get a value between specific chars in a string? [duplicate]

This question already has answers here:
C# Splitting Strings on `#` character
(3 answers)
Closed 7 years ago.
I got a string like, "house1_508_17.5_end003". Basically I want get 508, 17.5 out of this string.
You can split on the _ character.
string s = "house1_508_17.5_end003";
string[] digits = s.Split('_');
foreach (string digit in digits)
{
Console.WriteLine(digit);
}

How can I split a string by some characters and not by a string made of these characters? [duplicate]

This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 7 years ago.
How can I split a string by '-' and '>' and not by "->"?
I would like to split the string below:
AAA-BBB->CCC>DDD
and get the result equal to:
{ "AAA", "BBB->CCC", "DDD" }
The following example uses a regular expression with lookahead and lookbehind rules to split a string based on '-' or '>' but not '->':
string input = "AAA-BBB->CCC>DDD";
var regex = new Regex("-(?!>)|(?<!-)>");
var split = regex.Split(input);
// split = { "AAA, "BBB->CCC", "DDD" }

Removing characters from a string with lengths greater than 2 [duplicate]

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

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

Categories

Resources