3

Java question here: If i have a string "a", how can I "add" value to the string, so I get a "b" and so on? like "a++"

hogni89
  • 1,920
  • 6
  • 22
  • 39
  • 1
    What do you suppose you would get if you do `"ab"+1;`? – Haozhun Feb 23 '11 at 13:13
  • I think he wants a routine with returns the next letter in the alphabet – Johan Sjöberg Feb 23 '11 at 13:14
  • 1
    I get "ab1" .. I'm just talking about converting a string "a" to a "b", and a "b" to a "c". Just like when you do ++ on a int – hogni89 Feb 23 '11 at 13:17
  • 1
    possible duplicate of [How to increment a java String through all the possibilities?](http://stackoverflow.com/questions/342052/how-to-increment-a-java-string-through-all-the-possibilities) – dogbane Feb 23 '11 at 13:19

6 Answers6

12
String str = "abcde";
System.out.println(getIncrementedString(str));

Output

bcdef

//this code will give next char in unicode sequence

public static String getIncrementedString(String str){
        StringBuilder sb = new StringBuilder();
        for(char c:str.toCharArray()){
            sb.append(++c);
        }
        return sb.toString();
    }
Grundlefleck
  • 124,925
  • 25
  • 94
  • 111
jmj
  • 237,923
  • 42
  • 401
  • 438
4

If you use the char primitive data type you can accomplish this:

char letter = 'a';
letter++;
System.out.println(letter);

prints out b

Mark
  • 16,772
  • 9
  • 42
  • 55
3

i made some changes to te paulo eberman code, to handle digits and characters, if valuable for someone i share this mod....

public final static char MIN_DIGIT = '0';
public final static char MAX_DIGIT = '9';
public final static char MIN_LETTER = 'A';
public final static char MAX_LETTER = 'Z';

public String incrementedAlpha(String original) {
    StringBuilder buf = new StringBuilder(original);
    //int index = buf.length() -1;
    int i = buf.length() - 1;
    //while(index >= 0) {
    while (i >= 0) {
        char c = buf.charAt(i);
        c++;
        // revisar si es numero
        if ((c - 1) >= MIN_LETTER && (c - 1) <= MAX_LETTER) {
            if (c > MAX_LETTER) { // overflow, carry one
                buf.setCharAt(i, MIN_LETTER);
                i--;
                continue;
            }

        } else {
            if (c > MAX_DIGIT) { // overflow, carry one
                buf.setCharAt(i, MIN_DIGIT);
                i--;
                continue;
            }
        }
        // revisar si es numero
        buf.setCharAt(i, c);
        return buf.toString();
    }
    // overflow at the first "digit", need to add one more digit
    buf.insert(0, MIN_DIGIT);
    return buf.toString();
}

i hope be usefull for someone.

1

use this code to increment char value by an integer

int a='a';
System.out.println("int: "+a);
a=a+3;
char c=(char)a;
System.out.println("char :"+c);
tvshajeer
  • 1,299
  • 2
  • 12
  • 26
0
  • Convert the string to a char.
  • Increment the char.
  • Convert the char back to a string.

Example:

    //convert a single letter string to char
    String a = "a";
    char tmp = a.charAt(0);

    //increment char
    tmp++;

    //convert char to string
    String b = String.valueOf(tmp);
    System.out.println(b);
dogbane
  • 266,786
  • 75
  • 396
  • 414
0

Assuming you want something like aaab => aaac and not => bbbc, this would work:

public String incremented(String original) {
    StringBuilder buf = new StringBuilder(original);
    int index = buf.length() -1;
    while(index >= 0) {
       char c = buf.charAt(i);
       c++;
       buf.setCharAt(i, c);
       if(c == 0) { // overflow, carry one
          i--;
          continue;
       }
       return buf.toString();
    }
    // overflow at the first "digit", need to add one more digit
    buf.insert(0, '\1');
    return buf.toString();
}

This treats all characters (in fact char values) the same, and fails (does strange stuff) for some unicode code-points outside the first plane (which occupy two char values in a String).

If you only want to use english lowercase letters as digits, you can try this variant:

public final static char MIN_DIGIT = 'a';
public final static char MAX_DIGIT = 'z';

public String incrementedAlpha(String original) {
    StringBuilder buf = new StringBuilder(original);
    int index = buf.length() -1;
    while(index >= 0) {
       char c = buf.charAt(i);
       c++;
       if(c > MAX_DIGIT) { // overflow, carry one
          buf.setCharAt(i, MIN_DIGIT);
          i--;
          continue;
       }
       buf.setCharAt(i, c);
       return buf.toString();
    }
    // overflow at the first "digit", need to add one more digit
    buf.insert(0, MIN_DIGIT);
    return buf.toString();
}

This does a => b => c, y => z => aa => ab.

if you want to do more calculation with the string, consider staying with StringBuilder (or StringBuffer for multithreaded access) instead of repeatedly copying between String and StringBuilder. Or use a class made to do this, like BigInteger.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Thanks. I have figured everything out :) I'm working from front of string, to end. Done my own method. Works like a charm :) – hogni89 Feb 23 '11 at 13:56
  • okay, so you are using little-endian, compared to my big-endian. Could you post your variant in an own answer? – Paŭlo Ebermann Feb 23 '11 at 14:13