Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use StringBuffer in Java to complete replacement characters

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to use StringBuffer in Java to complete the replacement character", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use StringBuffer in Java to complete the replacement character" article can help you solve your doubts.

In this example, in order to ensure that the Java file name entered by the student is qualified, the StringBuffer class will be used to replace the special symbols in the Java file name, including "in the Chinese state." And full-width "." Replace it with "." in English.

In the implementation of this example, the character search and replacement methods of the StringBuffer class are mainly used, and the implementation code is as follows:

Import java.util.Scanner;public class test2 {public static void main (String [] args) {System.out.println ("enter the name of the Java file you want to submit:"); Scanner input = new Scanner (System.in); String fileName = input.next () / / get the java file name entered by the user / / define the StringBuffer object, and the string content is the java file name StringBuffer file = new StringBuffer (fileName) entered by the user; / / get the "." Whether there is int index = file.lastIndexOf ("."); / / judge the "in the Chinese state." And "." Whether there is int errIndex1 = file.lastIndexOf (".") ; int errIndex2 = file.lastIndexOf ("."); if (index! =-1 & & file.substring (index + 1, file.length ()) .equals ("java")) {System.out.println ("Job submitted successfully!") } else if (errIndex1! =-1 & & file.substring (errIndex1 + 1, file.length ()) .equals ("java")) {/ / the Chinese state of "." Change to "." in English. File.setCharAt (errIndex1,'.'); System.out.println ("your writing error has been corrected to:" + file + "submitted successfully!") ;} else if (errIndex2! =-1 & & file.substring (errIndex2 + 1, file.length ()) .equals ("java") {/ / the "." Change to "." in English. File.setCharAt (errIndex2,'.'); System.out.println ("your writing error has been corrected to:" + file + "submitted successfully!") ;} else {System.out.println ("the name of the java file you provided is incorrect, please verify!") ;}

Run the program when the name of the Java file entered by the user is "in Chinese". And "." Change it to "." in the English state, and then submit it, as follows:

Please enter the name of the Java file you want to submit:

Myexapmle . Java

Your writing error has been corrected to: myexample.java

In this program, the steps to replace special characters are as follows:

A variable of type String is defined and assigned to the name of the Java file entered by the user.

Use the StringBuffer (String str) constructor of StringBuffer to take the value of this variable as the value of the StringBuffer object.

Gets the position in the user input string (English and Chinese).

Use multiple if selection statements to determine whether the string entered by the user contains the string in the English state, and whether the following string is "java". If the condition is true, it means that the information entered by the user is correct and the submission is successful.

Determine in else if whether the string entered by the user contains the "in Chinese state." And. "and". " And the following string is "java", and if the judgment passes, the setCharAt () method in the StringBuffer class is called to modify "." For ".", then submit.

Supplement: replace specific position characters with StringBuffer

Import java.util.*;public class Solution {public static void main (String [] args) {Scanner sc = new Scanner (System.in); while (sc.hasNextLine ()) {StringBuffer s=new StringBuffer (); s.append (sc.nextLine ()); if (s.length () > = 1&&s.length ())

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report