Answers for "changing a character in a string java"

1

how to change a character in a string in java with ascii

import java.text.ParseException;
import java.util.Arrays;

/**
 * How to convert a String to ASCII bytes in Java
 * 
 * @author WINDOWS 8
 */

public class StringToASCII {

    public static void main(String args[]) throws ParseException {
        
        // converting character to ASCII value in Java
        char A = 'A';
        int ascii = A;
        System.out.println("ASCII value of 'A' is  : " + ascii);
        
        // you can explicitly cast also
        char a = 'a';
        int value = (int) a;
        System.out.println("ASCII value of 'a' is  : " + value);
        
        
        
        
        // converting String to ASCII value in Java
        try {
            String text = "ABCDEFGHIJKLMNOP";

            // translating text String to 7 bit ASCII encoding
            byte[] bytes = text.getBytes("US-ASCII");
            
            System.out.println("ASCII value of " + text + " is following");
            System.out.println(Arrays.toString(bytes));
            
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}

Output
ASCII value of 'A' is  : 65
ASCII value of 'a' is  : 97
ASCII value of ABCDEFGHIJKLMNOP is following
[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]


Read more: https://javarevisited.blogspot.com/2015/07/how-to-convert-string-or-char-to-ascii-example.html#ixzz6k2vn7o4y
Posted by: Guest on January-20-2021
3

replace character in string java

String str = ".............................."; 

        int index = 5; 
  
        char ch = '|'; 
 
        StringBuilder string = new StringBuilder(str); 
        string.setCharAt(index, ch); 

        System.out.println(string);
Posted by: Guest on August-14-2020

Code answers related to "changing a character in a string java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language