2011-02-23 90 views
2

Java问题在这里: 如果我有一个字符串“a”,如何“添加”值的字符串,所以我得到一个“b”等? 像 “A ++”增加字符串值

+1

你怎么想,如果你这样做'“AB” +1,你会得到递增字符值;'? – Haozhun 2011-02-23 13:13:19

+0

我想他想要一个例程,并返回字母表中的下一个字母 – 2011-02-23 13:14:18

+1

我得到“ab1”..我只是谈论将字符串“a”转换为“b”,将“b”转换为“c ”。就像当你在一个int上执行++ – hogni89 2011-02-23 13:17:21

回答

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

输出

BCDEF

//如果你使用这个代码将会给下一个字符以Unicode序列

public static String getIncrementedString(String str){ 
     StringBuilder sb = new StringBuilder(); 
     for(char c:str.toCharArray()){ 
      sb.append(++c); 
     } 
     return sb.toString(); 
    } 
+2

我认为正确的单词会*增加*没有'a'。 – 2011-02-23 13:24:37

+1

@user我希望这是你要求的 – 2011-02-23 13:29:03

+0

Thaks。奇迹般有效! – hogni89 2011-02-23 13:59:17

4

您可以交流的char原始数据类型complish这样的:

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

打印输出B

0
  • 将字符串的字符。
  • 递增字符。
  • 将char 转换回字符串。

例子:

//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); 
+0

这适用于此示例,但不适用于'ab'。 – 2011-02-23 13:46:27

0

假设你想要的东西,像aaab =>aaac,而不是=>bbbc,这会工作:

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(); 
} 

这会将所有字符(其实char值)相同,并失败(奇怪的东西)一些unicode代码点以外的第一个平面(它占用字符串中的两个char值)。

如果你只想使用英文小写字母为数字,你可以试试这个变种:

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(); 
} 

这确实a =>b =>cy =>z =>aa =>ab

如果你想用字符串做更多的计算,可以考虑使用StringBuilder(或者StringBuffer进行多线程访问),而不是在String和StringBuilder之间重复复制。 或者使用一个类来做这个,比如BigInteger。

+0

谢谢。我已经想通了:)我从字符串的前面工作到结束。完成我自己的方法。工程就像一个魅力:) – hogni89 2011-02-23 13:56:51

+0

好吧,所以你使用little-endian,相比我的big-endian。你可以在自己的答案中发布你的变体吗? – 2011-02-23 14:13:11

2

我做了一些改动对TE圣保罗eberman代码,来处理数字和字符,如果有价值的人我分享这个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(); 
} 

我希望成为有用的人。

1

使用此代码通过一个整数

int a='a'; 
System.out.println("int: "+a); 
a=a+3; 
char c=(char)a; 
System.out.println("char :"+c);