Write a java program to modify a string.

Write a program to input a person’s name in the format “FirstName LastName” and return the person’s name in the following format – “LastName, InitialOfFirstName”.

Input and Output Format

  • Input consists of a string that corresponds to a Person’s name.
  • Output consists of a string(person’s name in expected format).

Refer sample output for formatting specifications

Sample Input :

Jessica Miller

Sample Output:

Miller, J

Program to Modify a String in Java

Following are the steps to modify an input string into the above-given format:

  • Input string from the user.
  • Pass the string to getvalues() method.
  • Inside the method, create an empty string buffer to store the updated string.
  • Use the string tokenizer class to break the string into tokens and store each token in s2 and s3.
  • Now, append string s3 with the ‘,'(comma) and then append it to a string buffer.
  • Convert the string in s2 to upper case and extract only the first character using the subString(0,1) method.
  • At last, append them to string buffer and print it.
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    String s1 = sc.nextLine();
    getvalues(s1);
  }

  public static void getvalues(String s1) {
    StringBuffer sb = new StringBuffer();
    StringTokenizer st = new StringTokenizer(s1, " ");
    String s2 = st.nextToken();
    String s3 = st.nextToken();
    sb.append(s3).append(",");
    sb.append(s2.substring(0, 1).toUpperCase());
    System.out.println(sb);
  }
}

Output

Modify a String using replace(), replaceFirst() and replaceAll() method

In Java, we have so many built-in functions that make things easy to use to modify strings. With the help of this function, we can modify certain characters in a string.

replace() method:

This method replaces every occurrence of a given character with a new character and returns a new string.

replace(oldChar, newChar);

replaceFirst() method:

This method replaces the first substring of this string that matches the given regular expression with the given replacement.

replaceFirst(String regex, String replacement)

replaceAll() method:

This method also replaces each substring of this string that matches the given regular expression with the given replacement.

replaceAll(String regex, String replacement)

Program to modify string using replace(), replaceFirst() and replaceAll() method

In this program we are modifying the strings in the following ways:

  • First, with the help of replace() method, we will replace the occurrence of every ‘h’ character with the ‘t’.
  • Secondly, with the help of the replaceFirst() method, we will replace only the first occurrence of ‘h’  with ‘k’.
  • At last, with the help of the replaceAll() method, we would replace all the numbers in a given string.
package com.demo3;

import java.util.Scanner;
import java.util.StringTokenizer;

public class StringModify {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        //Using replace() method : replace h with t
        String s2 = s1.replace('h','t');
        System.out.println("Modify String using replace() method: "+s2);
        //Using replaceFirst() method : It will replace first occurrence with replace character
        System.out.println("Modify String using replaceFirst() method: "+s1.replaceFirst("h","k"));
        //Using replaceAll() method: It will remove all the numbers.
        System.out.println("Modify String using replaceAll() method: "+s1.replaceAll("\\d",""));

    }
}

Output

Thus, in this way, we learn how to modify a string in a given format in Java.