I get the line: Ivan 5 rubles 20 kopecks. (the value in bold is always different) How to break it all into 3 variables: name = Ivan; rub = 5 rubles; kop = 20 kopecks.

  • Is there a separator between the values? - Bald
  • Split , cycle, conditions, plus int.TryParse . Carefully put everything together. It is possible to loop without multiple if . - nick_n_a
  • Bald, just a space. sometimes the full name is met - Petrov Ivan Ivanovich - user222349
  • nick_n_a, you can give an example, but it is a little incomprehensible - user222349
  • one
    Give your piece of code, and that you do not understand. The f-tion int.TryParse separates numbers from words. In my opinion, this task must be done independently. - nick_n_a

3 answers 3

If the structure is always the same, then you can go from the end, it will be the easiest way.

 String str = "Мне все равно как этого парня зовут 1 рубль 1 копейка"; String[] split = str.Split(' '); Int32 splitCount = split.Count(); string kop = String.Join(" ", split.Skip(splitCount - 2)); string rub = String.Join(" ", split.Skip(splitCount - 4).Take(2)); string name = String.Join(" ", split.Take(splitCount - 4)); 
  • Your method works, only sometimes it is found 12-23,26,28 kopecks (( - user222349
  • Everything, he figured it out. Thanks - user222349

You can use regulars. For example:

 (?'name'.*?) (?'rub'\d+ рублей) (?'kop'\d+ копеек) 

Example

  • one
    and if there is 1 ruble? - tCode
  • This will add to the regular season or | - nick_n_a
  • 2
    the question indicates that the variable values ​​are bold. The word "rubles" is not bold; therefore, it is logical to assume that it is immutable. If this is not the case, then modifying it is quite simple by adding through | literal ruble as above advises @nick_n_a - DreamChild
  • @DreamChild and the fact that the name may change does not bother you? - tCode
  • one
    @tCode And this is not a question for me "ok or not ok." This is a topstarter. Is it somewhere in the problem said about the morphology? You overcomplicate too. In this case, you will need to write a parser that takes into account the rules for the coordination of numerals and nouns in Russian. As a last resort, you can replace the literals "rubles" and "kopecks" with groups of any characters - DreamChild
 string[] s = String.Split(' '); name = s[0]; rub = s[1] + " " + s[2]; kop = s[3] + " " + s[4];