Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java replace character in string

String s = "new String";
String replaced = s.replace("new","Test");
							  ^		 ^
                             old    new char
Comment

how to change single character of a string in java

//short answer: you cannot individually change any specific character
//of a String in java. You can however do this:

String s1 = "This is a String";
String s2 = s1.substring(0, 8) + "o" + s1.substring(9);
System.out.println(s2);
//Prints "This is o String", replaced the 8th character with an o
Comment

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); 
Comment

how to change the character of a string in 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);
  }
}
Comment

how to replace a character with another character in a string in 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"));
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: read file from resources java 
Java :: java question mark operator 
Java :: java swing get frame size 
Java :: java how to print out a string in uppercase 
Java :: java string character at index 
Java :: Divide two integers without using multiplication, division and mod operator 
Java :: convert calendar to date java 
Java :: remove duplicates from string in java 
Java :: java boucle for 
Java :: java compute sum and average of array elements 
Java :: java structure example 
Java :: initialize hashmap java 
Java :: is it possible to quick sort a string in java 
Java :: index of an array procesing 
Java :: arraylist vs vector in java 
Java :: android java how to blur an image 
Java :: if checkbox checked java 
Java :: Java get integer color from rgb 
Java :: how to find armstrong numbers in java 
Java :: creating an array list in java 
Java :: what is constructor in java 
Java :: How to implement the A* shortest path algorithm, in Java? 
Java :: java return new instance of generic type 
Java :: finding length of arrays in java 
Java :: spring mvc project example 
Java :: JAVA Character Literals 
Java :: java stream sort Collator 
Java :: java stream map int to char 
Java :: javafx get button id 
Java :: char value java 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =