How to remove last word separated by a dot (...)

Israel

Active member
Joined
Jan 10, 2020
Messages
26
Programming Experience
Beginner
Hi,
My codes need some corrections.
I have into my textbox numbers separated by dot (for example: 1.5.6.7), then I need to remove the last number with the dot before (for example colored in red: 1.5.6.7) . Curiosly its works ONLY separating by space not by dot (for exampe: 1 5 6 7). This number should be fixed only inside of texbox.

C#:
private string strcut(string str)
{
 string[] a = str.Trim().Split(' ');
 string str1 = string.Empty;
 for (int i = 0; i < a.Count() - 1; i++)
 {
 str1 = str1 + a[i];
 if (a.Count() - 2 != i)
 { str1 += " "; }
}
 return str1;
}
 
private void btnTest_Click(object sender, EventArgs e)
{
 string str = textBox1.Text;
 MessageBox.Show(strcut(str));
 
Of course it only works on spaces, because you're telling to work on spaces. There's nothing curious about it. I suspect that that is not your code and that you have simply copied it off the internet with no understanding of how it works. If you understand that code then it should be obvious where the spaces are being detected and so where you need to make it look for dots instead. If you don't understand the code then I suggest that you take some time to gain some understanding. Copying code off the internet and then getting strangers to edit it for you doesn't really qualify as writing code. If that is your code then you just need to change it to look for dots where you told it to look for spaces.
 
Of course it only works on spaces, because you're telling to work on spaces. There's nothing curious about it. I suspect that that is not your code and that you have simply copied it off the internet with no understanding of how it works. If you understand that code then it should be obvious where the spaces are being detected and so where you need to make it look for dots instead. If you don't understand the code then I suggest that you take some time to gain some understanding. Copying code off the internet and then getting strangers to edit it for you doesn't really qualify as writing code. If that is your code then you just need to change it to look for dots where you told it to look for spaces.

It's true. I had thinking a lot how to resolv it. That why I went to the Net to find if can I have some solutions. Then I found out these codes.
Anyway I do resolv it by myself like this. Maybe can help anyone in the same situation.

C#:
private string strcut(string str)
{
string[] a = str.Trim().Split('.'); //Adding a dot here
string str1 = string.Empty;
for (int i = 0; i < a.Count() - 1; i++)
{
str1 = str1 + a[i];
if (a.Count() - 2 != i)
{ str1 += "."; } //Adding a dot also here
}
return str1;
}

private void btnTest_Click(object sender, EventArgs e)
{
string str = textBox1.Text;
//MessageBox.Show(strcut(str));
textBox1.Text = strcut(str);
 
For the record, you can use string.Join to perform the inverse operation to string.Split. There are various overloads and the one for you is the one that let's you specify how many elements from an array to join:
C#:
var temp = input.Split('.');
var output = string.Join(".", temp, 0, temp.Length - 1);
 
Why bother with all that code if all you want to do is return the numbers without the last number and last dot before the last number, you should use substring. Since you are only passing in a string, it's rather pointless. I'd only use split here If I was using a continuation of a Linq query.
C#:
        private string strcut(string str)
        {
            string r = str.Substring(0, str.Length - 2);
            return r;
        }
 
Your current code breaks down the numbers you sent into the method, and strips each number and stores it in a string[] without the dot. [ . ]

You then do a pointless iteration only to build up the numbers with the dots being added when you could simply substring and take whatever variation of the number you want by specifying its start and end indexes.
 
Back
Top Bottom