Copy a string until character found in Java

24,584

Use String#indexOf(int) and String#substring(int).

String input = "hello.1234";
String output = input.substring(0, input.indexOf('.'));
Share:
24,584
nuvio
Author by

nuvio

Updated on August 16, 2020

Comments

  • nuvio
    nuvio over 3 years

    Hello I was wandering how can I copy a piece of a String in Java until a character/symbol is found. In my case the original string is: "hello.1234" and I want only "hello", so that every thing after the symbol . is discarded.

    Any idea? many thanks

    EDIT:

    solved as follows:

    String partBeforeFullStop = input.split("\\.")[0];
    
  • nuvio
    nuvio over 11 years
    this thing does the opposite of what I wanted :S
  • Baz
    Baz over 11 years
    Use input.substring(0, input.indexOf('.')); instead.