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);
Related
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.
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:
How to get distinct characters?
(9 answers)
Closed 8 years ago.
I have a 5 character long string from Console.Readline() and I want to test if all 5 characters are different.
You can use Distinct method which will give you the distinct characters then just compare the count with the input length, if they are equal that means all chars are different.
string input = Console.ReadLine();
bool isDifferent = input.Distinct().Count() == input.Length;
Note you need using System.Linq; to use Distinct method.
This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
string mystring="the are boys";
string[] tags = {"the"};
string[] replace ={"they"}
mystring.Replace(tags[0],replace[0]) // is not working
mystring.Replace("the","they") // is working
I thought both are same but first statement is not working. The second one is.
Please help me to solve the problem.
I assume that you don't assign the return value of String.Replace to the variable. But since strings are immutable you have to do that:
mystring = mystring.Replace(tags[0],replace[0])
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Split String into smaller Strings by length variable
I have seen solutions that split strings by a certain character but I wanted to seperate a string every certain amount of characters. Does anyone know how to do this?
its been asked
Split String into smaller Strings by length variable