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

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

Related

collapsing series of spaces [duplicate]

This question already has answers here:
How do I replace multiple spaces with a single space in C#?
(28 answers)
How to replace multiple white spaces with one white space
(17 answers)
Trying to replace all white space with a single space
(2 answers)
c# Fastest way to remove extra white spaces
(29 answers)
How to replace multiple spaces with single space?
(2 answers)
Closed 3 years ago.
Refactoring existing code.
They are trying to collapse any series of spaces in string passed in to a single space.
Surely there is a better way.
for (int i = 0; i < 25; i++)
{
str = str.Replace(" ", " ");
}
System.Text.RegularExpressions.Regex.Replace(str,#"\s+"," ");
Split the string using
List<string> spiltList = yourStr.Split(' ').ToList();
Remove empty string from collection.
spiltList.RemoveAll(e => string.IsNullOrWhiteSpace(e));
Join the list of strings to a single string
string result = string.Join(" ", spiltList);

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

Create string[] from string with ampersand [duplicate]

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

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

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