Home c# C # Delete repeating characters from line

C # Delete repeating characters from line

Author

Date

Category

is available, let’s say, 5 textboxes. And one main. As those five textboxes are filling out, the name is in the main thing. Between the textbox values ​​are used by the signs of the lower underscore "_" .

For example:

textbox1: 123
Textbox2: 456.
Textbox3: 789.
TextBox4: AA
TextBox5: BB

Chief Texbox Display: 123_456_789_AA_BB

What is the problem: to create a name does not always need to fill all the textsboxes. Sometimes one or two enough.
Then we see the following: 123_456 ___ or __ 789 __

How can you do in the line in the presence of two in a row and lower underscores only one thing remained. Based on the examples, it would take 123_456 and 789 .

besides, here for example indicated that they are 5. In reality there may be 10-15.

that tried: TRIM () methods , replace () , remove () .
But I did not find solutions. Either removes the necessary or not correctly triggered.

Update:

To form a final name I use a similar structure:

string filename = tb_1.text + "_" + tb_2.text + "_" + tb_3.text + " _ "+ TB_4.Text +" _ "+ cb_1.text +" _ "+ cb_2.text +" _ "+ cb_3.text +" _ "+ cb_4.text +" _ "+ cb_6.text +" _ " + TB_5.TEXT + "_" + TB_6.Text + "_" + TB_7.Text;
TB_FULLNAME.TEXT = FILENAME;

Maybe there is a simpler and correct way to fill, so that you should not remove the lower underscore?


Answer 1, Authority 100%

For example.

take the list

list & lt; string & gt; Lines = New List & LT; String & GT; ();

Add all the necessary values ​​to it

lines.add (textbox1.text);
Lines.add (textbox2.text);
// ...

Further you have 2 options:

or immediately use Linq

string result = string.join ("_", lines.where (S = & gt; s? .length & gt ; 0));

or pre-imperatively in the cycle you need to delete empty strings

for (int i = 0; i & lt; lines.count; i ++)
{
  if (string.isnullRempty (Lines [i]))
  {
    Lines.Removeat (I);
    I--;
  }
}
String Result = String.Join ("_", Lines);

That is, this task is reduced to prevent the appearance of these duplicates, and not competently remove them.


one-stroke for your specific case, using an array instead of a list, but no difference.

tb_fullname.text = string.join ("_", new string [] {tb_1.text, tb_2.text , TB_3.Text, TB_4.Text, CB_1.TEXT, CB_2.Text, CB_3.Text, CB_4.Text, CB_6.Text, TB_5.Text, TB_6.Text, TB_7.Text} .where (S = & GT; S? .Length & gt; 0));

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions