How do I increment a variable to the next or previous letter in the alphabet?

前端 未结 5 630
别跟我提以往
别跟我提以往 2021-02-04 03:48

I have a capital letter defined in a variable string, and I want to output the next and previous letters in the alphabet. For example, if the variable was equal to \'C\'

5条回答
  •  悲&欢浪女
    2021-02-04 04:25

    One way:

    String value = "C";
    int charValue = value.charAt(0);
    String next = String.valueOf( (char) (charValue + 1));
    System.out.println(next);
    

提交回复
热议问题