开发者

How can I set a specific positon in a String in Java?

I h开发者_运维问答ave a String with name str.

str = "hi john";

Now I want to set j char to g. How can I do that?


You can't modify a String directly, but you can either use a StringBuilder:

str = "hi john";
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(3,'g');
str = sb.toString();    

.. or convert it to a char[] and back

str = "hi john";
char[] chars = str.toCharArray();
chars[3] = 'g';
str = new String(chars);


Two ways :

a. This will replace all occurances of 'j' with 'g'.

String str1 = "hi john";    
System.out.println(str1);  // prints - hi john  

String str2 = str1.replace('j', 'g');  
System.out.println(str2);  // prints - hi gohn  

b. If you wish to change the 'j' character only at one location in the string, you may want to do like this.

String str4 = replaceCharAt("hi john", 3,'g');  

public static String replaceCharAt(String str1, int pos, char c) {
  StringBuffer buf = new StringBuffer(str1);
  buf.setCharAt(pos,c);
  return buf.toString( );
}  

// Here : pos = 3, char = 'g' and str1 = "hi john"


str = str.replace('j', 'g'); should do it for you.


str.replace('j','g');

as this java api shows http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replace(char, char)


you can do like this


String str ="hi john"
syso(str.replace('j','g'));

Output will be hi gohn


You can use the substring. Look at :http://download.oracle.com/javase/tutorial/java/data/manipstrings.html


You can use something like this:

public static String replace(String _text, String _searchStr, String _replacementStr) {
    // String buffer to store str
    StringBuffer sb = new StringBuffer();

    // Search for search
    int searchStringPos = _text.indexOf(_searchStr);
    int startPos = 0;
    int searchStringLength = _searchStr.length();

    // Iterate to add string
    while (searchStringPos != -1) {
    sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
    startPos = searchStringPos + searchStringLength;
    searchStringPos = _text.indexOf(_searchStr, startPos);
    }

    // Create string
    sb.append(_text.substring(startPos,_text.length()));

    return sb.toString();
    }

public static int indexOf(String sb, String str, int start){
    int index = -1;
    if((start>=sb.length() || start<-1) || str.length()<=0) return index;
    char[] tofind = str.toCharArray();
    outer: for(;start<sb.length(); start++){
        char c = sb.charAt(start);
        if(c==tofind[0]){
            if(1==tofind.length) return start;
            inner: for(int i = 1; i<tofind.length;i++){ // start on the 2nd character
                char find = tofind[i];
                int currentSourceIndex = start+i;
                if(currentSourceIndex<sb.length()){
                    char source = sb.charAt(start+i);
                    if(find==source){
                        if(i==tofind.length-1){
                            return start;
                        }
                        continue inner;
                    } else {
                        start++;
                        continue outer;
                    }
                } else {
                    return -1;
                }

            }
        }
    }
    return index;
}


String s;
int i = str.indexOf('j');
s = str.subString(0, i) + 'g' + str.subString(i + 1, str.length() - 1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜