TextBox2 has this kind of text:

Wohnort:Schweiz Entfernung:690 km Beruf:Ingenieurswesen 

How to copy the word Schweiz после Wohnort , чтобы в TextBox1 so было только слово Schweiz` appears in TextBox1?

Update

I did not understand your question, but I just need to move the text to something like this:

 string Town = TextBox2.Text;//TextBox2.Text Ячейка со всей информацией Town = Town.Substring(Town.IndexOf("Wohnort:"));// Нужные мне данные находяться после этого слова. Town = Town.Remove(Town.IndexOf("Entfernung:"));// Это слово я поставил для того чтобы мне в нужную ячейку не закидывал всё что после.. Town = Town.Replace("Wohnort:", ""); //Удаляю Начальное слово чтобы тоже не попадало в нужную мне ячейку TextBox1.Text = (Town); 

but sometimes the word "Entfernung:" may be missing and be Something else. So it must be somehow different.

All this works for me, I showed how, But I Am sure that all this can be written more simply in three lines, something like:

 string Town = TextBox2.Text; Town.Substring(Town.IndexOf("Wohnort:")); Town = Town.ХЗ(Town.Хз("[^ ]Мать его Одно Слово до пробела")) 

Any thoughts?

Answer:

You wrote that you get the text in TextBox2 using regular expressions, add some kind of character to the end of each found result, say “_” you will get an expression of this kind:

 Wohnort:Schweiz__ Entfernung:690 km__ Beruf:Ingenieurswesen__ 

And now you can turn your action in this way.


 string Town = TextBox2.Text; Town = Town.Substring(Town.IndexOf("Wohnort:")); Town = Town.Replace("Wohnort:", ""); TextBox1.Text = Town.Remove(Town.IndexOf("_")); 

Now it doesn't matter if your second word is from

 Town = Town.Remove(Town.IndexOf("Entfernung:")); 

or some other you have a symbol from which to make a start.

but in this way:

 if (userinformbox.Text.Contains("Wohnort:")) { string Town = TextBox2.Text;; Town = Town.Substring(Town.IndexOf("Wohnort:")); Town = Town.Replace("Wohnort:", ""); TextBox1.Text; = Town.Remove(Town.IndexOf("_")); } 

It doesn't even matter to you whether the position in question will be at all, if not, go to the next one. If you have any questions, write, how can I help. ----------

 ВОТ ТАК ВОТ ("Вашу " + "Cами дальше додумывайте кого"); ------------------------ 
  • one
    Are you sure that the name and the existence of the control of an unknown framework named TextBox1 is relevant to the point? - VladD
  • Updated the question - Rubenovich
  • > I do not understand your question: what is the difference, what is the name of your textbox? Whether it is TextBox1, TextBox2 or something else, what does this have to do with the task itself? - DreamChild
  • Yes, it does not really make any difference, just for example wrote TextBox1 - Rubenovich
  • Good. then specifically describe what and by what rules you need to copy. So far your question looks very vague: “you need to copy one word after another word, which may not be there or something else may be in its place”. By what criteria should the program understand what to copy and from where? - DreamChild

1 answer 1

I will not tell you with a regular expression, because I am not strong in them, but this can be turned in the “usual” way. If I understand your condition correctly, then you need something like this:

 const string source = "Wohnort:Schweiz Entfernung:690 km Beruf:Ingenieurswesen"; const string firstWord = "Wohnort"; const string secdondWord = "Entfernung"; string res = ""; if(source.Contains(firstWord)) { int pos = source.IndexOf(firstWord); res = pos != -1 ? source.Substring(pos + firstWord.Length) : ""; } else { var dividers = new[] { ':', ',', ' ', '.' }; if (source.IndexOf(secdondWord) != -1) { int firstPos = source.IndexOf(secdondWord) + secdondWord.Length + 1; string substr = source.Substring(firstPos); var positions = dividers.Select(x => substr.IndexOf(x)) .Where(x => x != -1); res = positions.Any() ? source.Substring(firstPos, positions.Min()) : ""; } else res = ""; } Console.WriteLine(res); 
  • I have everything in WinForms, I'm sorry, but I can't translate this way, I can't get to ask together "Wohnort: Schweiz Entfernung: 690 km Beruf: Ingenieurswesen"; Textbox I Need - Rubenovich
  • You cannot substitute the string TextBox1.Text instead of the string called source. In this case, it makes sense for you to familiarize yourself with the basics of the language - DreamChild
  • So People, you complicate things for me, I have it all working, I showed how, But I am Sure that all this can be written more simply in three lines, something like: string Town = TextBox2.Text; Town = Town.Substring (Town.IndexOf ("Wohnort:")); Town = Town. HZ (Town. Xs ("[^] Mother of his Word to the Space")) Are there any thoughts? - Rubenovich
  • @Rubenovich: You should be able to write a string to a textbox and read it from there. If you do not know how - do not disgrace, go read the documentation and come later. The rest and the most difficult - string conversion - you were shown in the answer. And yes, what you need is not done by a single magical challenge. - VladD
  • Well, look at him! Right now, I principe I will do what I have in mind, And then I will throw the option Here and then I will tell someone what to learn. AAAAAAAA !!!!!!!!!!!!!! - Rubenovich