This question already has answers here:
C# string splitting
(4 answers)
Closed 7 years ago.
I need to be able to extract the three strings separated by a dashes "-" from an input string.
For example:
Mystring="54-0-9";
the values separated by "-" are with unknown length because they should be an input for the user.
The user should enter each value in a textField than my code concatenate the 3 values and put it as shown . Later i want to get the three values separated again each one in a new text field. How can i do that in c# !?
Using string split.
// Select on "-".
string[] split = _string.Split(new Char[] { '-' });
split[0], split[1], split[2] will have the values you want.
Mystring="54-0-9";
Mystring.Split('-');
this will give you a array of 3 now.
Related
This question already has answers here:
Removing everything after the first " " in a string? (space)
(4 answers)
Closed 9 months ago.
My Problem is that my Users sometimes did mistakes and i want to avoid this by removing the splitted part of the string automatically.
i dont want to combine the splitted string, i just want to remove all what is written after the space.
Should look like this:
All what i selected should be automatically removed after i press the Button to Save/Confirm.
Source: Removing everything after the first " " in a string? (space)
It would be simpler to do a string.Split():
string input = "some:kind of string that I want totrim please.";
input = input.Split()[0];
//input = "some:kind" now
So it should look something like this, based on what are you working on (I suppose WPF)
string inputText = txtInput.Text;
inputText.Split()[0];
This question already has answers here:
Check if a string contains one of 10 characters
(6 answers)
Closed 2 years ago.
I have this array:
char[] signOfDecimal = {',', '.'};
and I'm trying to find the location of one of these 2 characters in my string:
locationOfDotInRate = myString.IndexOf(signOfDecimal[0], signOfDecimal[1]);
Basically I'm trying to check where one of these characters might occur in my string, but its not working. The reason I'm doing it this way is because my string sometimes contain '.' or ','.
You can use IndexOfAny method
locationOfDotInRate = myString.IndexOfAny(signOfDecimal);
This question already has answers here:
How to remove the last comma and space from string (if there) using regex?
(4 answers)
Closed 7 years ago.
I have a string that can end will several "X" characters.
Let's say the string ends in commas like this.
string X = "1,2,3,,,";
I need some function or lambda expression that can remove the last set of commas.
I will have no way of knowing how many commas there are at the end of the string but the end result should look like this.
?x -- "1,2,3";
You dont need regex or LINQ, use String.TrimEnd:
X = X.TrimEnd(',');
You can use it with multiple characters, f.e. if you want to remove trailing commas and dots:
X = X.TrimEnd(',', '.');
This question already has answers here:
Easiest way to parse a comma delimited string to some kind of object I can loop through to access the individual values?
(7 answers)
Closed 8 years ago.
What would be the most elegant way to extract values from a string of comma-separated numbers?
It should account for whitespaces too. Example:
Input string: "22,10,8,13"
Input string: "22, 10, 8, 13"
Input string: "22, 10 ,8 ,13,"
Given all three inputs, the method should recognise the values.
Example output for all three inputs: "String contains: 22 10 8 13"
Possible duplicate: Easiest way to parse a comma delimited string to some kind of object I can loop through to access the individual values?
string input = "22,10 ,8 , 13";
var output = input.Split(',').Select(i=>i.Trim());
This question already has answers here:
Parse strings to double with comma and point
(14 answers)
Closed 8 years ago.
I am trying to read a number from the console, but if it is input via comma (ex. 2,56), it cannot be parsed successfully. It can be parsed only if it is input like this: 2.56
How can I change that?
Here is a sample code:
if (double.TryParse(stringElements[i], out doubleNum))
{
averageTime.Add(doubleNum);
}
replace the comma by '.' like this :
save your number in variable X , then
if (double.TryParse((X).Replace(",", "."), out tmp))
{
}
replace with "."
Convert.ToDouble("2,56".Replace(",", "."));