How to remove all chars after a space in Textbox [duplicate] - c#

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

Related

c# how to do an action if any character string does NOT match a pattern? [duplicate]

This question already has answers here:
Fastest way to check if string contains only digits in C#
(21 answers)
Closed 2 years ago.
I have a pattern and want to empty the textbox and show a message when the input does not match the pattern.
I have tried putting the erasing line in else, and used a negation ! in front of the condition.
if (!Regex.IsMatch(MyTextBox.Text, ".*?[0-9].*?"))
{
MyTextBox.Text = "";
MessageBox.Show("Please input only numbers");
}
But both of these solution result in that if ANY symbol in the textbox matches the pattern gives an error when I want to float.parse the string later on.
Examples:
1234 should and does work
A Stops you as it should
1A Doesn't stop you, but should
Try to use this:
if (!Regex.IsMatch(textbox.Text, "^[0-9]*$")){
....
}

How to remove a specific character, From...To the End of string in c# [duplicate]

This question already has answers here:
How to get the first five character of a String
(22 answers)
Closed 2 years ago.
I have a string like this: first/exemple/import/status/5b12f918-f2ef-436f-808c-50ea48da000
I need to the output like this: first/exemple/import/
I need to remove from status and so on. I thought about remplace but it wont work because this 5b12f918-f2ef-436f-808c-50ea48da000 is variable so it changes.
if there any function can do this please
If you can guarantee the structure of the text (ie that structure won't change):
public static ReturnFirst(string inString){
var path = inString.Split('//')[1];
return $"//{path}//";
}
Or read the first 'x' characters of the string

String.Insert not working [duplicate]

This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 4 years ago.
I have (hopefully) a simple problem.
String.Insert(0, "test");
is not working, yet:
String.Text += "test";
does..? I need Insert so I can insert something in the middle of a string.
I'm such an idiot, instant Google search fixed this.
String.Insert does not modify the string it is called on, but instead returns a new string with the inserted text in.
So I needed:
String = String.Insert(0, "test");
it should work
see the below example
string names = "Romeo Juliet";
string shakespeare = names.Insert(6, "and ");
Console.WriteLine(shakespeare);
the result will be Romeo and Juliet
see the fiddle

Find an unknown string between two known characters [duplicate]

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.

Remove last "X" characters from string [duplicate]

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

Categories