fork(1) download
  1. using static System.Console;
  2.  
  3. public class Test {
  4. public static void Main() {
  5. const int MaxTamanhoEndereco = 30;
  6. WriteLine(TrimPosition("Rua Aventureiros Afortunados, 12345", MaxTamanhoEndereco));
  7. WriteLine(TrimPosition("Av Aventureiros Afortunados, 12345", MaxTamanhoEndereco));
  8. WriteLine(TrimPosition("R Aventureiros Afortunados, 12345", MaxTamanhoEndereco));
  9. WriteLine(TrimPosition(" Aventureiros Afortunados, 12345", MaxTamanhoEndereco));
  10. WriteLine(TrimPosition("Aventureiros Afortunados, 12345", MaxTamanhoEndereco));
  11. WriteLine(TrimPosition("Aventureiros Afortunados, 1234", MaxTamanhoEndereco));
  12. }
  13. static string TrimPosition(string texto, int limite) {
  14. if (texto.Length > limite) {
  15. var lastChar = texto[limite - 3];
  16. var isSpecial = lastChar == ' ' || lastChar == '.' || lastChar == ',';
  17. texto = texto.Substring(0, limite - 3);
  18. var posicaoUltimoEspaco = texto.LastIndexOf(' ');
  19. if (posicaoUltimoEspaco > 0 && !isSpecial) texto = texto.Substring(0, posicaoUltimoEspaco);
  20. texto += "...";
  21. }
  22. return texto;
  23. }
  24. }
  25.  
  26. //https://pt.stackoverflow.com/q/114959/101
Success #stdin #stdout 0.03s 24496KB
stdin
Standard input is empty
stdout
Rua Aventureiros...
Av Aventureiros Afortunados...
R Aventureiros Afortunados,...
 Aventureiros Afortunados,...
Aventureiros Afortunados,...
Aventureiros Afortunados, 1234