Java Basic Problems

Java String Manipulation: How to search or match any given word in a string and then replace that word

search a word in a string and then replace javaHere, I am sharing you another basic technique about java where I have searched a word in a String and then replace it by another word. In this technique I have avoided case differences (avoiding upper case or lower case letter difference). From this example you will be able to know about

  1. How to search any specific word in a string
  2. 

  3. How to avoid case difference  while searching a word in a string using java
  4. How to replace any specific word ignoring case difference.

 

Continue reading “Java String Manipulation: How to search or match any given word in a string and then replace that word”

Java Basic Problems

Java: Break String into individual Character

In String manipulation sometimes we may need to break the string into each separate or individual character.

In java we can do it easily using  toCharArray() function.

In the given example, string   “Java Programming ”  is broken down into characters where you can see that the length of that string is 16 (including the space between  “Java” and “Programming”) and the whole string is broken into each individual character , where we have stored that characters into  an array.

Here is the code to convert  string into array:

class StringBreaker

{

StringBreaker()

{

String s=”Java Programming”;

char[] allchars=new char[s.length()];  

allchars = s.toCharArray();

System.out.println(“****************************”);

System.out.println(“\n”);

 System.out.println(“Length of S:”+s.length());

System.out.println(“\n”);

 for (int n=0; n<s.length(); n++)

{

try

            {

            System.out.println(“Character::”+(n+1)+”::”+allchars[n]);

            }

catch(Exception e){}

              }

 System.out.println(“\n”);

 System.out.println(“****************************”);

}

public static void main(String args[])

{

StringBreaker test=new StringBreaker();

}

}

OUTPUT: