0
public class ChessMethod {
  public static void main(String[] args) {

      String line=" 1.d4 Nf6 2.c4 g6  0-1]";
      String[] elements = line.split("[\\s.]+");
      String first = elements[1];
      String[] trailing = Arrays.copyOfRange(elements,1,elements.length);

      System.out.println(Arrays.deepToString(trailing));

  }
}

Output= [1, d4, Nf6, 2, c4, g6, 0-1]]

I would like to get rid of the "1.","2." part of the move list and only keep the

d4, Nf6... I was going to add them to a list.

Is there a better way to do this with a delimiter? Do I have to make a method that checks the (if there is one) numerical value of every String in my Array, and saves the ones that dont have one?

2
  • Use "\\s*\\d+\\.\\s*" as delimiter, then split on whitespace? Commented May 21, 2017 at 23:15
  • This is a great help! Thanks! Commented May 21, 2017 at 23:21

1 Answer 1

2
String line=" 1.d4 Nf6 2.c4 g6  0-1]";
        String[] elements = line.split("[\\s.]+");
        List<String> sts  = new ArrayList<>();
        for(String st : elements) {
            if(st.matches(".*[a-z].*"))
                sts.add(st);
        }
        System.out.println(sts);

This ouputs

[d4, Nf6, c4, g6]

May be this is what you want, if I am not missing anything

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I was looking for! at the if statement, is that regex or a delimiter? What is the difference between them. Thanks!
st.matches(".*[a-z].*") checks if the string contains any char between a-z. If you want the add capital char also .. you can just change it to [a-zA-Z]
thats regex.... that just searching for any char between a-z like I mentioned in prev comment. Let me know if you have any other question. Mark the answer correct please if this helped. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.