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

What are the ways to reverse the specified part in a Java string

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the ways to reverse the specified part of the Java string". The explanation in the article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "what are the ways to reverse the specified part of the Java string".

Convert "asdfghj" to "ashgfdj"

Method 1: convert a string to char []

Call the toCharArray () method in the String class to convert the string to characters, and then use the for () loop to reverse the specified characters at the beginning and end of the index.

Public class StringDemo {public static void main (String [] args) {StringDemo stringDemo = new StringDemo (); String str1 = "asdfghj"; String str2 = stringDemo.reverse1 (str1,2,5); System.out.println ("inverted to:" + str2);} public String reverse1 (String str, int start, int end) {if (str! = null) {char [] charArray = str.toCharArray () For (int I = start, j = end; I

< j; i++, j--) { char temp = charArray[i]; charArray[i] = charArray[j]; charArray[j] = temp; } return new String(charArray); } return null; }} 运行结果: 反转后为:ashgfdj 方式二:使用String的拼接 将字符串分割为三部分:需反转字符串前,需反转字符串,需反转字符串之后,调用substring()方法截取字符串,循环倒序遍历需反转字符串部分,后拼接这三部分 public static void main(String[] args) { StringDemo stringDemo = new StringDemo(); String str1 = "asdfghj"; String str2 = stringDemo.reverse1(str1,2,5); System.out.println("反转后为:" + str2); } public String reverse1(String str, int start, int end) { // 第一部分 String newStr = str.substring(0, start); // 第二部分 for (int i = end; i >

= start; str.charAt -) {newStr + = str.charAt (I);} / / part III newStr + = str.substring (end + 1); return newStr;}

Running result:

After inversion: ashgfdj

Method 3: replace String with StringBuffer/StringBuilder

Compared with the improvement of mode 2, it is more efficient to use the append () method in Stringbuilder to replace "+ =" in mode 2.

Public class StringDemo {public static void main (String [] args) {StringDemo stringDemo = new StringDemo (); String str1 = "asdfghj"; String str2 = stringDemo.reverse1 (str1,2,5); System.out.println ("inverted to:" + str2);} public String reverse1 (String str, int start, int end) {StringBuffer s = new StringBuffer (str.length ()) / / part I s.append (str.substring (0, start)); / / part II for (int I = end; I > = start; I muri -) {s.append (str.charAt (I));} / / part III s.append (str.substring (end + 1)); return s.toString ();}}

Running result:

After inversion: ashgfdj

Note: in mode 3, it is only a string reversal operation, and there is no need to consider thread safety, so StringBuilder is preferred.

Examples of actual combat are attached

Rotate the specified part of the string, such as str= "mmabcdfeffnn", and specify the abcdfeff part to rotate, resulting in the string str= "mmffefdcbann".

The idea of its implementation is like this: divide the source string into three parts, head, to be rotated, and tail, all of which need to be intercepted, and for the part to be rotated, a technique is adopted here, that is to borrow StringBuffer (String string) as an intermediary, and call the reverse () method to rotate. The final three parts: head, rotated, tail, connect concat (String string) operation.

The code is as follows:

/ * the specified part of the string is reversed * @ author zhonglinsen * 2016-3-1 PM * / public class StringReverse {public static void main (String [] args) {String str= "mmabcdefnn"; int start=2; int end=7; System.out.println ("original string:" + str); String strNew=strReverse (str, start, end) System.out.println ("string rotated at the specified position:" + strNew);} public static String strReverse (String strArg,int start,int end) {String strStart=strArg.substring (0quotient start); String strEnd=strArg.substring (end+1,strArg.length ()); String strSub=strArg.substring (start, end+1); StringBuffer sb=new StringBuffer (strSub); strSub=sb.reverse () .toString () Return strStart.concat (strSub) .concat (strEnd);}} Thank you for your reading. The above is the content of "what is the way to reverse the specified part of the Java string?" after the study of this article, I believe you have a more profound understanding of the way to reverse the specified part of the Java string, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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