This question already has answers here:
How to identify if a string contains more than one instance of a specific character?
(4 answers)
Closed 8 years ago.
I think my question is simple.
I just need a little character check when I click in one button. If such textbox have more than one comma, I want a message: "Error. Please insert just one comma in the box."
I was searching but have not found anything like this. Can someone help me? Thanks all in advance!
According to this POST:
You can compare IndexOf to LastIndexOf to check if there is more than one specific character in a string without explicit counting:
var s = "12121.23.2";
var ch = '.';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
In your case:
var s = "Comma, another comma, something.";
var ch = ',';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
You can set a boolean when you check if there is a another comma in the string (Textbox.Text).
As listed above here is another POST concerning your question.
As stated in the comments, you can use .Count(). Look HERE for more.
Related
This question already has an answer here:
ASCII.GetString() stop on null character
(1 answer)
Closed 5 years ago.
So I am working on a little program that helps you sort and view images.
I am currently trying to load the Exif Data and display it.
The conversion and loading works, but the problem is when I am trying to concatenate the camera and the maker's name, the label only displays the first string.
EDIT:
Found the problem thanks to #Kevin Gosse
Make sure that the strings (especially the first one) does not end with a new line character. The safest way to do this is to use TrimEnd().
It's also useful to extract these explicitly into separate variables, check the values for validity, and then construct the rest of the logic:
// data gathering & pre-processing
string makerName = data.MakerName.TrimEnd('\r', '\n');
string camModel = data.CameraModel.TrimEnd('\r', '\n');
//build UI text
string labelText = makerName + " - " + camModel;
//update UI
theLabel.Text = labelText;
As it turns out, the String contained a null character \0 which caused the label to get rendered without the subsequent text. I fixed it by removing the last char of the string. (Solution suggested by #KevinGosse in comments.)
This question already has answers here:
string.split - by multiple character delimiter
(6 answers)
Closed 6 years ago.
Hey guys I have a quick question. I am trying to parse a single string into multiple strings using a keyword. I can currently find a set of code that allows me to parse the single string but I need to store each of the new strings as a new variable/item.
string full = "Hey//Please//Help";
parse into:
first string- Hey
Second string- Please
third string- Help
So for example I would like to manipulate the first string, Hey, by itself. Please let me know if any more explanation is necessary.
Simple String.Split:
string[] results = full.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
foreach(string str in results)
{
Console.WriteLine(str);
}
Result:
Hey
Please
Help
This question already has answers here:
How to read RegEx Captures in C#
(3 answers)
Closed 7 years ago.
First of all sorry about the tittle. Didn't know how to explain it.
This is my code:
string sString = #"docs/horaires/1/images/1"
var PickImage = Regex.Matches(sString, "/horaires/(.*?)/images/");
Console.WriteLine(PickImage[0].Value);
This will print /horaires/1/images/ instead of 1. I tried all the RegexOptions but didn't find the solution in there.
What am I doing wrong here?
This is what you need:
string sString = #"docs/horaires/1/images/1";
var pickImage = Regex.Match(sString, #"/horaires/(.*?)/images/");
if (pickImage.Success)
Console.WriteLine(pickImage.Groups[1].Value);
In your original code, PickImage[0] is a Match object, and Value will return the full match. You want the first captured group, so use match.Groups[1].Value. Note that Groups[0] always contains the full match.
No need to use Matches is you want a single result, use Match instead.
This question already has answers here:
Trim last character from a string
(15 answers)
Closed 8 years ago.
I have string like below.
string s="this is item1,item2,item3,,, ,, ,";
now i want to remove (,) from right side of string.
Thanks in advance
i have tried
string.Replace(",", "");
and
string.TrimEnd("'");
but not working
Try this:
s = s.TrimEnd(',', ' ');
I think the problem in your code is that you do not assign the result of your replacement to any variable. And also your solution would remove all , from the string, not only those on the right side.
Try string.TrimEnd():
s= s.TrimEnd(',',' ');
This question already has answers here:
C# string replacement , not working
(6 answers)
Closed 8 years ago.
I have a string like
mukesh "salaria" engineer
how do i replace " with blank
like i want output as
mukesh salaria engineer
I already tried, str.replace("\"",string.empty);
but it doesn't working for me.
It works but must type this: str = str.replace(...)
string.Replace is a pure method(no side-effects). If you do not assign str.replace("\"",string.empty); to anything, then that statement does not make any change in the state of your object(in other words writing that line of code is equal to not writing it at all.).
Use str = str.Replace(...);
You must use the Replace method and get the string back from it. Your code must be:
str = str.Replace("\"", string.Empty);
msdn :
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
try this
str.Replace('\"',' ');