Kadov Posted January 26, 2016 at 12:07 PM Report Share #592544 Posted January 26, 2016 at 12:07 PM Olá, turma do fórum. Estou encarando um pequeno probleminha. Não sei como copiar um pedaço da string até um determinado caracter da própria string. Por exemplo, a string é: AbcAbcAbcCde. Como eu faria para copiar tudo até o 3º "c" ? Sem contar as posições fixas das letras. Pois a mesmas poderiam mudar. Link to comment Share on other sites More sharing options...
He B TeMy Posted January 26, 2016 at 12:14 PM Report Share #592546 Posted January 26, 2016 at 12:14 PM (edited) Dim temp as Integer = string.indexof('c') temp = string.indexof('c', temp +1) Dim resultado as integer = string.indexof('c', temp +1) Lida com erros a não ser que saibas que a string têm três 'c' . Edited January 26, 2016 at 12:23 PM by He B TeMy Identificar correctamente o bloco de código! Link to comment Share on other sites More sharing options...
Kadov Posted January 26, 2016 at 12:21 PM Author Report Share #592547 Posted January 26, 2016 at 12:21 PM (edited) Não funcionou muito bem. O resultado me mostrado é -1. Aplicado ao exemplo exposto. Como proceder? Edited January 26, 2016 at 12:22 PM by Kadov Link to comment Share on other sites More sharing options...
He B TeMy Posted January 26, 2016 at 12:23 PM Report Share #592549 Posted January 26, 2016 at 12:23 PM Fiz uma edit acima, vê se dá. Para copiares tens de fazer substring, aquilo é só para veres até que index copiar. Link to comment Share on other sites More sharing options...
ribeiro55 Posted January 26, 2016 at 12:31 PM Report Share #592550 Posted January 26, 2016 at 12:31 PM Podes generalizar a coisa com uma funçãozita não-recursiva, para conseguires entender: (só precisas da Function CadeiaAte. O resto é contexto de uma ConsoleApp, para teres a "full picture") Module Module1 Sub Main() Dim cadeia As String = "AbcAbcAbcCde" Console.WriteLine(CadeiaAte(cadeia, "c", 3)) Console.ReadKey() End Sub Public Function CadeiaAte(cadeia As String, caractere As String, repeticao As Integer) If cadeia.ToCharArray().Count(Function(c) c = "c") < repeticao Then Return cadeia End If Dim Resultado As String = String.Empty For Each c As Char In cadeia.ToCharArray() If repeticao = 0 Then Exit For If c.ToString() = caractere Then repeticao -= 1 Resultado &= c.ToString() Next Return Resultado End Function End Module Se usares muito até podes fazer uma extensão para a classe String, que depois usas directamente nas strings, por exemplo: Dim cadeia As String = "AbcAbcAbcCde" cadeia.CadeiaAte("c",3) 1 Report Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
Kadov Posted January 26, 2016 at 12:58 PM Author Report Share #592552 Posted January 26, 2016 at 12:58 PM He B TeMy, tentei novamente e não obtive sucesso. ribeiro55, obrigado!! Pelo seu método, funcionou!! 🙂 Pode dar closed. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now