StringBuilder get,delete,set char
In this chapter you will learn:
- How to get a single char from StringBuilder
- How to change a single char for StringBuilder
- How to delete a char at specified index
Get a single char from StringBuilder
char charAt(int index)
returns the char value in this sequence at the specified index.
public class Main {
public static void main(String[] argv) {
StringBuilder sb = new StringBuilder();
sb.append("java2s.com");
/*from jav a 2s .c om*/
char ch = sb.charAt(0);
System.out.println(ch);
}
}
The output:
Change a single char for StringBuilder
void setCharAt(int index, char ch)
sets char at the specified index.
public class Main {
public static void main(String[] argv) {
StringBuilder sb = new StringBuilder();
sb.append("java2s.com");
/*from java 2 s. co m*/
sb.setCharAt(0, 'J');
System.out.println(sb.toString());
}
}
The output:
Delete a char at specified index
StringBuilder deleteCharAt(int index)
removes the char at the specified position in this sequence.
public class Main {
public static void main(String[] argv) {
StringBuilder sb = new StringBuilder();
sb.append("java2s.com");
//from j av a 2 s. c o m
sb.deleteCharAt(0);
System.out.println(sb.toString());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » String