“Ganti karakter dalam string java” Kode Jawaban

String ganti java

public static void main(String args[]){  
String s1="my name is khan my name is java";  
String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was"  
System.out.println(replaceString);  
}}  
Fierce Fly

string java ganti karakter pada posisi

String str = in.nextLine();	//Original String
char cr = in.next().charAt(0); // character to replace
int index = in.nextInt();	// Index where replaced
str = str.substring(0, index) + cr + str.substring(index + 1);// modified string
SpaceNinja

Ganti karakter dalam string java

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

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

        System.out.println(string); 
Important Impala

cara mengubah karakter string di java

// You cannot change the characters of a string in java
// But you can make it work
// For Example: A Program to change 'r' to 'a'
class Main{
  public static void main(String args[]){
    String str = "return";
    String res = "";
    for(int i = 0; i<str.length(); i++){
      if(str.charAt(i)=='r'){
        res+='a';
      }
      else{
        res+=str.charAt(i);
      }
    }
    System.out.print("Result = "+res);
  }
}
Wide-eyed Warbler

cara mengganti karakter dengan karakter lain dalam string di java

public class JavaExample{
   public static void main(String args[]){
	String str = new String("Site is BeginnersBook.com");

	System.out.print("String after replacing com with net :" );
	System.out.println(str.replaceFirst("com", "net"));

	System.out.print("String after replacing Site name:" );
	System.out.println(str.replaceFirst("Beginners(.*)", "XYZ.com"));
   }
}
devops unicorn

Jawaban yang mirip dengan “Ganti karakter dalam string java”

Pertanyaan yang mirip dengan “Ganti karakter dalam string java”

Lebih banyak jawaban terkait untuk “Ganti karakter dalam string java” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya