How to take a String value before a Special character

Asked

Viewed 14,347 times

3

Hello, good morning. I have a little doubt, how can I get the value of a String before some special character. For example, Clinica Antonio S/S. I would like to take only the Clinica Antonio S, which comes after the bar does not need to be picked up. Can anyone help? Thanks

  • What about the phrase 'I don’t know! It’s cool! '? Your question is vague, describe it better with more examples!

  • What I want is to go through a string until I find a special character. Example Clinica Antonio S/S, I want to go through the "/" and take only what has before the /, which in the case is Clinica Antonio S

4 answers

5


You can do it this way:

//String a ser analisada
String Str = new String("Clinica Antonio S/S");
//Posição do caracter na string
int pos = Str.indexOf("/");
//Substring iniciando em 0 até posição do caracter especial
System.out.println(Str.substring(0, pos) );

See an example Ideone

  • in this case would only work for the character '/'.

  • @Renarosantos, yes it is a simple example about the problem presented, and I believe that the language is Java and not Javascript

  • Thank you very much, based on your answer I managed to solve the problem. Thank you to all who have reappeared also.

4

You can use the function StringTokenizer contained in java.lang.Object

For example:

public class Demonstracao{
   public static void main(String[] args){
      // Cria um StringTokenizer passando como parâmetro a sua string
      StringTokenizer st = new StringTokenizer("Ola/Mundo");

      // Verifica o próximo token
      System.out.println("Proximo token: " + st.nextToken("/"));
      System.out.println("Proximo token: " + st.nextToken("/"));
   }    
}

In this case the output would be:

Proximo token: Ola
Proximo token: Mundo
  • 1

    Showing an example of how to use will make your answer better.

0

One of the ways to do it, in Javascript is the following:

<script>
 function myFunction() {
     var str = "E ai mané.Tudo bem?";
     var n = str.search(/á|é|í|ó|ú|(|)/); ///coloque aqui todos os caracteres especiais
     var a = str.substr(0,n); /// var a = 'E ai man'
 }
</script>

In other programming languages, just follow the same logic.

0

var s = "Clinica Antonio S/S"

s.split(/[^\w\s]]/,1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.