none
Delete the last character of first word and the last character of last word in a textbox

    Question

  • Hello 

    I have a textbox with three words and i need to delete the last character of first word and the last character of last word , this is my code :

       string mytext=txt_search.Text.ToString();
             string[] words = {mytext};
           
             foreach (string word in words) 
             {
                 string teste = word.TrimEnd('s');
                 Label1.Text = teste;
             }

    this is working just for the last word ...

    Any ideas ?

    Thanks 

    Tuesday, October 30, 2012 6:32 PM

Answers

  • Maybe you can use something like this

    string c = "lots of codes"; var q = c.Select(item => item).Where(row => !row.ToString().Contains("s")); var newString = string.Concat(q);

    Keep in mind that will remove only the letter 's' if you have other phrase, different than this, you should use the first method that i provide.


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer



    Tuesday, October 30, 2012 6:49 PM
  • var words = txt_search.Text.Split(' ');

    var lastIndex = words.Length;

    var first = "";

    var last = "";

    if (lastIndex > 0 )

    {

        first = words[0].Substring(0, words[0].Length-1);

        last = words[lastIndex].Substring(0, words[0].Length-1);

       // do something

    }

    Tuesday, October 30, 2012 6:40 PM

All replies

  • Thats because you dont make a Split(), try this one

                string mytext = "roger that";
                string[] words = mytext.Split(' ');
    
                foreach (string word in words)
                {
                    string teste = word.Remove(word.Length - 1, 1);
                    Console.WriteLine(teste);
                }

    The output will be: roge
                                   tha


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Tuesday, October 30, 2012 6:35 PM
  • var words = txt_search.Text.Split(' ');

    var lastIndex = words.Length;

    var first = "";

    var last = "";

    if (lastIndex > 0 )

    {

        first = words[0].Substring(0, words[0].Length-1);

        last = words[lastIndex].Substring(0, words[0].Length-1);

       // do something

    }

    Tuesday, October 30, 2012 6:40 PM
  • my problem now is that i need to cut just the the character 's' in both words 

    an example: 

    lots of codes 

    i need to get just 

    lot of code

    Thanks in advance .

    Tuesday, October 30, 2012 6:44 PM
  • Maybe you can use something like this

    string c = "lots of codes"; var q = c.Select(item => item).Where(row => !row.ToString().Contains("s")); var newString = string.Concat(q);

    Keep in mind that will remove only the letter 's' if you have other phrase, different than this, you should use the first method that i provide.


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer



    Tuesday, October 30, 2012 6:49 PM
  •  last = words[lastIndex].Substring(0, words[0].Length-1);

    says

    Index was outside the bounds of the array.

    can´t solve that yet ...


    Tiago

    Tuesday, October 30, 2012 8:08 PM
  • my problem now is that i need to cut just the the character 's' in both words 

    .


    You want to remove a final 's' (and no other character?) from the first and last word? If the only word separator is the space, you can split, check that the last character is 's' and remove it:
    var words = txt_search.Text.Split(' ');
    if (words.Length > 0 )
    {
        if(words[0].EndsWith("s"))
            words[0] = words[0].Remove(words[0].Length - 2);
    }
    if (words.Length > 1 )
    {
        if(words[lastIndex].EndsWith("s"))
            words[1] = words[lastIndex].Remove(words[0].Length - 2);
    }
    txt_search.Text = string.Join(" ", words);

    You could also use a regular expression:
    txt_search.Text = Regex.Replace(txt_search.Text, @"(?<=^\w*)s\b|s$", "");


    • Edited by Louis.fr Wednesday, October 31, 2012 8:42 AM
    Wednesday, October 31, 2012 8:41 AM
  • Thats because you dont make a Split(), try this one

                string mytext = "roger that";
                string[] words = mytext.Split(' ');
    
                foreach (string word in words)
                {
                    string teste = word.Remove(word.Length - 1, 1);
                    Console.WriteLine(teste);
                }

    The output will be: roge
                                   tha


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer


    That would remove the last character from every word, not from the first and last word.
    Wednesday, October 31, 2012 8:43 AM
  • Maybe you can use something like this

    string c = "lots of codes"; var q = c.Select(item => item).Where(row => !row.ToString().Contains("s")); var newString = string.Concat(q);

    Keep in mind that will remove only the letter 's' if you have other phrase, different than this, you should use the first method that i provide.

    There are a lot of things to say about that code.

    - it removes all 's' characters from the input, from every word and in every position;

    - 'row' is not a really good choice of variable name for a character;

    - to check that row is not the character 's', you can write it row!='s' instead of calling ToString and Contains.

    Wednesday, October 31, 2012 8:48 AM
  • my problem now is that i need to cut just the the character 's' in both words 

    an example: 

    lots of codes 

    i need to get just 

    lot of code

    Thanks in advance .

    what if there is S present in the middle words? I guess Michael's option is best suited for you.

    regards

    joon

    Wednesday, October 31, 2012 9:13 AM
  • Sorry for taking so long , i need to check all  the words in a string  and check if it ends with 's', and yes the word separator is the space .

    I´m thinking on the use of pluralization singularization service of .net framework 4.0 ....  


    Tiago

    Wednesday, October 31, 2012 3:45 PM
  • That would remove the last character from every word, not from the first and last word.

    And that is exactly that the OP asks.

    Sorry for taking so long , i need to check all the words in a string and check if it ends with 's', and yes the word separator is the space . I´m thinking on the use of pluralization singularization service of .net framework 4.0 ....

    This will work as expected

                string mytext = "rogers osf thats";
                string[] words = mytext.Split(' ');
                string test = string.Empty;
    
                foreach (string word in words)
                {
                    if (word.ToLower().EndsWith("s"))
                        test = word.Remove(word.Length - 1, 1);
                    else
                        test = word;
                    Console.WriteLine(test);
                }


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Wednesday, October 31, 2012 4:10 PM
  • That would remove the last character from every word, not from the first and last word.

    And that is exactly that the OP asks.

    Now, yes. But, read the original post: "i need to delete the last character of first word and the last character of last word".

    I keep my computer and my crystal ball in two distinct rooms. Sorry.

    Wednesday, October 31, 2012 4:56 PM
  • Sorry I explained badly my problem , regex solved the problem for first and last word thank you for your time .

    But my aim now is to remove all character 's'  at the end of the words in all words typed in my textbox .


    Tiago

    Thursday, November 01, 2012 10:02 PM
  • To remove all 's' characters from the end of every word, you can use last code from Norkk.

     You could also use a regex (it will be much simpler than the last one):

    txt_search.Text = Regex.Replace(txt_search.Text, @"s\b", "");

    It exactly means "remove all occurences of 's' at the end of words (\b)"

    Friday, November 02, 2012 3:16 PM
  • Thanks Louis.fr Regex.Replace did the job , while using Norkk last code i just get the last word without the 's' my code :

     	string mytext =txt_search.Text;
            string[] words = mytext.Split(' ');
            string test = string.Empty;
    
            foreach (string word in words)
            {
                if (word.ToLower().EndsWith("s"))
                {
                    test = word.Remove(word.Length - 1, 1);
                    txt_search.Text = test.ToString();
                }
    
                else
                {
                    test = word;
                    
                }
               
            }


    Tiago

    Monday, November 05, 2012 3:37 PM