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);
}
Related
This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 5 months ago.
I will create a parametric structure
I will take after each '#' with its extension.
For example here I need to get two texts after #
string text = "#sdvdsv0..-+mk?/!|°¬ *$%&()oO###sdfv684618awer6816";
string[] results = Regex.Split(text, "#+").Where(s => s != String.Empty).ToArray<string>();
Full example on a .net 6.0 console:
using System.Text.RegularExpressions;
string text = "#sdvdsv0..-+mk?/!|°¬ *$%&()oO###sdfv684618awer6816";
string[] results = Regex.Split(text, "#+").Where(s => s != String.Empty).ToArray<string>();
foreach (var x in results)
{
Console.WriteLine(x);
}
Output:
sdvdsv0..-+mk?/!|°¬ *$%&()oO
sdfv684618awer6816
Use "#" to match only a single # character.
Use "#+" to match at least once with the # character
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 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" }
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);
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));