UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Write a programme to replace the string without using replace command? This is my string. ..... s is replaced by th output is Thith ith my thtring

Asked by Last Modified  

Follow 0
Answer

Please enter your answer

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; import java.util.*; /** * * @author AP Edusoft */ public class Logic { /** ...
read more
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; import java.util.*; /** * * @author AP Edusoft */ public class Logic { /** * @param args the command line arguments */ public static void main(String[] args) { String originalString = "This is the cat"; String replacedString = replaceMethod(originalString, "the", "your"); System.out.println(replacedString); } static String replaceMethod(String str, String from, String to) { String[] arr = str.split(from); StringBuilder output = new StringBuilder(); int i = 0; for (; i < arr.length - 1; i++) output.append(arr[i]).append(to); output.append(arr[i]); if (str.substring(str.lastIndexOf(" ")).equalsIgnoreCase(" " + from)) output.append(to); return output.toString(); } } read less
Comments

Java Professional with 11+ Yrs of experience

Reverse String Method 1: 1. The user will input the string to be reversed. 2. First we will convert String to character array by using the built in java String class method toCharArray(). 3. Then , we will scan the string from end to start, and print the character one by one. Reverse String...
read more
Reverse String Method 1: 1. The user will input the string to be reversed. 2. First we will convert String to character array by using the built in java String class method toCharArray(). 3. Then , we will scan the string from end to start, and print the character one by one. Reverse String Method 2: 1. In the second method , we will use the built in reverse() method of the StringBuilder class ,. Note : String class does not have reverse() method . So we need to convert the input string to StringBuilder , which is achieved by using the append method of the StringBuilder. 2. After that print out the characters of the reversed string by scanning from the first till the last index. Reverse String Method 3: 1. Convert the input string into character array by using the toCharArray() built in method of the String Class . 2. In this method we will scan the character array from both sides , that is from the start index (left) as well as from last index(right) simultaneously. 3. Set the left index equal to 0 and right index equal to the length of the string -1. 4. Swap the characters of the start index scanning with the last index scanning one by one .After that increase the left index by 1 (left++) and decrease the right by 1 i.e (right--) to move on to the next characters in the character array . 5. Continue till left is less than or equal to the right . Reverse String Method 4: 1. Convert the input string into the character array by using toCharArray() built in method. reverse a string in java with example 2. Then add the characters of the array into the LinkedList object . We used LinkedList because it maintains the insertion order of the input values. 3. Java also has built in reverse() method for the Collections class . Since Collections class reverse() method takes a list object , to reverse the list , we will pass the LinkedList object which is a type of list of characters. 4. We will create the ListIterator object by using the listIterator() method on the LinkedList object. ListIterator object is used to iterate over the list. 5. ListIterator object will help us to iterate over the reversed list and print it one by one to the output screen. Reverse String Method 5 : 1. The last method is converting string into bytes . getBytes() method is used to convert the input string into bytes[]. 2. Then we will create a temporary byte[] of length equal to the length of the input string. 3. We will store the bytes(which we get by using getBytes() method) in reverse order into the temporary byte[] . read less
Comments

completeeducationhub.com web site you will get information

String str="This is my String"; String s=" "; for(int i=0;i
Comments

completeeducationhub.com web site you will get information

for(i=0;i
Comments

Java Technical Leader

Look at the program. If you face problem to understand this coding let me know. package com.test.scjp; public class StringReplacer { public String replaceMethod(String original, String toReplace, String replacedWith) { for(int j=0;j read more
Look at the program. If you face problem to understand this coding let me know. package com.test.scjp; public class StringReplacer { public String replaceMethod(String original, String toReplace, String replacedWith) { for(int j=0;jread less
Comments

Software Engineer

I have done this in javascript . i think u just only need to replace the var with string . var myStr = "Thith ith my thtring"; var newStr = ""; for(var i=0;i
Comments

Software Engineer

I have done this in javascript . i think u just only need to replace the var with string . var myStr = "Thith ith my thtring"; var newStr = ""; for(var i=0;i
Comments

Java Tutor

It can be done using split/substring method..
Comments

Computer science tutor

String str="This is my String"; StringBuffer buffer=new StringBuffer(); for(int i=0;i read more
String str="This is my String"; StringBuffer buffer=new StringBuffer(); for(int i=0;iread less
Comments

MCA, BIT Mesra, Ranchi

Apart from all the basic ways, this can be done with the help of pattern(regular expressions) which in-turn applies String class replaceAll method: public static String replaceString(String sourceStr, String replaceString, String replaceBy) { return Pattern.compile(replaceString).matcher(sourceStr).replaceAll(replaceBy); } public...
read more
Apart from all the basic ways, this can be done with the help of pattern(regular expressions) which in-turn applies String class replaceAll method: public static String replaceString(String sourceStr, String replaceString, String replaceBy) { return Pattern.compile(replaceString).matcher(sourceStr).replaceAll(replaceBy); } public static void main(String[] args) { System.out.println(replaceString("This is my string", "s", "th")); } Output: Thith ith my thtring read less
Comments

View 12 more Answers

Related Questions

What are the qualities of a good Java trainer?
Good java trainers are always teaching their students with practical examples. This exactly happens in BEE Apprentice, our trainers are working in MNCs hence they are updated with technology and are best trainers.
Venu
0 0
5
Should we learn DBMS and RDBMS without any Java training?
java or i can say any programming language is not required to learn DBMS or RDBMS
Karthik
0 0
6
Why does â??5/2 = 2â?? and not 2.5 in Python?
Due to dynamic Typing. use Numpy , and declare data type , you will get desired result
Srikanth
0 0
6
Who can provide good Java training with real-time but not institute?
I provide industry specific training to students. If interested check my profile. I used to provide training in Big Data, Java, Scala and Spark too.
Sridevi
0 0
6
What is the use of package in JAVA?
Have you ever observed why we all are having kitchen / hall / bedroom or any? Have you ever observed why most of the bags contains zips and folders ? Have you ever observed why people create different...
Pushpendra

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Syntax and example of java
Java syntax: Class Display - - class definition { Void Display() { System.out.println('welcome to Java') ; } Public static void main(String arcs) { Display D=new Display() ;--object creation D. Display } }

10 Cool SQL Optimizations That Do Not Depend On The Cost Model
Today, we don’t want to talk about cost based optimisation, i.e. optimisations that depend on a database’s cost model. We’ll look into much simpler optimisations that can be implemented...

Thread Class In JAVA
Thread Class: Thread class is the main class on which Java's Multithreading system is based. Thread class, along with its companion interface Runnable will be used to create and run threads for utilizing...

Introduction to Course Content
Video about what we are going to learn throughout the Java Training Session .

ListIterator Example
public class ListIteratorEx { public static void main(String args) { List<String> li = new ArrayList<String>(); li.add("java1"); li.add("java2"); li.add("java3"); li.add("java4"); ListIterator...
S

Sarthak C.

0 0
0

Recommended Articles

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more