delphi64位 char数组转换string中文乱码_使用位运算、值交换等方式反转java字符串-共四种方法...
在本文中,我们将向您展示几种在Java中将String类型的字符串字母倒序的几种方法。
StringBuilder(str).reverse()
char[]
循环与值交换byte
循环与值交换- apache-commons-lang3
如果是为了进行开发,请选择StringBuilder(str).reverse()
API。出于学习的目的,我们可以研究char[]
和byte
方法,其中涉及到值互换和移位运算技术,这些技术对于了解StringBuilder(str).reverse()
API黑匣子背后原理非常有帮助。
1. StringBuilder(str).reverse()
在Java中,我们可以使用StringBuilder(str).reverse()
使字符串字母倒序。
public class ReverseString1 {public static void main(String[] args) {String str = "Reverse a String in Java";StringBuilder sb = new StringBuilder(str).reverse();System.out.println(sb.toString());}
}
输出结果
avaJ ni gnirtS a esreveR
2.char[]
首先,我们将字符串转换为char数组,并逐个循环char数组,并使用temp变量交换值。
public class ReverseString2 {public static void main(String[] args) {String str = "Hello World";System.out.println(reverse(str)); // dlroW olleH}public static String reverse(String input) {if (input == null || input.length() < 0)throw new IllegalArgumentException("Please provide an input!");char[] result = input.toCharArray();int startIndex = 0;int endIndex = result.length - 1;char temp;for (; endIndex > startIndex; startIndex++, endIndex--) {temp = result[startIndex];result[startIndex] = result[endIndex];result[endIndex] = temp;}return new String(result);}}
上面的算法需要5个循环(长度/ 2)来使字符串倒序“ Hello World”。
------------------------------------
H e l l o W o r l d
------------------------------------
0 1 2 3 4 5 6 7 8 9 10
------------------------------------Loop #1 - Swap index 0 <-> index 10
------------------------------------
{d} e l l o W o r l {H}
------------------------------------
{0} 1 2 3 4 5 6 7 8 9 {10}
------------------------------------Loop #2 - Swap index 1 <-> index 9
------------------------------------
d {l} l l o W o r {e} H
------------------------------------
0 {1} 2 3 4 5 6 7 8 {9} 10
------------------------------------Loop #3 - Swap index 2 <-> index 8
------------------------------------
d l {r} l o W o {l} e H
------------------------------------
0 1 {2} 3 4 5 6 7 {8} 9 10
------------------------------------Loop #4 - Swap index 3 <-> index 7
------------------------------------
d l r {o} o W {l} l e H
------------------------------------
0 1 2 {3} 4 5 6 {7} 8 9 10
------------------------------------Loop #5 - Swap index 4 <-> index 6
------------------------------------
d l r o {W} {o} l l e H
------------------------------------
0 1 2 3 {4} 5 {6} 7 8 9 10
------------------------------------
3. Byte[] – StringBuilder(str).reverse(str)
以下代码段类似于StringBuilder(str).reverse()
的内部实现(UTF16内容除外)。
import java.nio.charset.StandardCharsets;public class ReverseString3 {public static void main(String[] args) {String str = "Hello World";System.out.println(reverse(str));}public static String reverse(String input) {if (input == null || input.length() < 0)throw new IllegalArgumentException("Please provide an input!");byte[] val = input.getBytes(StandardCharsets.UTF_8);int length = val.length - 1;for (int start = (length - 1) >> 1; start >= 0; start--) {int end = length - start;byte temp = val[start];val[start] = val[end];val[end] = temp;// debugging//System.out.println(String.format("start=%s, end=%s", start, end));}return new String(val);}}
最令人困惑的部分是右移运算符(length - 1) >> 1
,这是什么意思?查看下面的8位示例,您可以找到规律吗?
System.out.println(10>>1); // 10 -> 5
0000 1010 = 10
0000 0101|0 = 10 >> 1 = 5System.out.println(4>>1); // 4 -> 2
0000 0100 = 4
0000 0010|0 = 4 >> 1 = 2System.out.println(100>>1); // 100 -> 50
0110 0100 = 100
00110 010|0 = 100 >> 1 = 50System.out.println(7>>1); // 7 -> 3
0000 0111 = 7
0000 0011|1 = 7 >> 1 = 3
对于数字,每右移1位,金额将减少该值的一半并四舍五入。这(length - 1) >> 1
试图找出字符串的中间点。
number >> 1 = round_down(number/2) or Math.flooa(number/2)
值交换从内部开始,然后扩展到外部。
for (int start = (length - 1) >> 1; start >= 0; start--) {int end = length - start;byte temp = val[start];val[start] = val[end];val[end] = temp;
}
上面的算法图示如下:
------------------------------------
H e l l o W o r l d
------------------------------------
0 1 2 3 4 5 6 7 8 9 10
------------------------------------Loop #1 - Swap index 4 <-> index 6
------------------------------------
H e l l {W} {o} o r l d
------------------------------------
0 1 2 3 {4} 5 {6} 7 8 9 10
------------------------------------Loop #2 - Swap index 3 <-> index 7
------------------------------------
H e l {o} W o {l} r l d
------------------------------------
0 1 2 {3} 4 5 6 {7} 8 9 10
------------------------------------Loop #3 - Swap index 2 <-> index 8
------------------------------------
H e {r} o W o l {l} l d
------------------------------------
0 1 {2} 3 4 5 6 7 {8} 9 10
------------------------------------Loop #4 - Swap index 1 <-> index 9
------------------------------------
H {l} r o W o l l {e} d
------------------------------------
0 {1} 2 3 4 5 6 7 8 {9} 10
------------------------------------Loop #5 - Swap index 0 <-> index 10
------------------------------------
{d} l r o W o l l e {H}
------------------------------------
{0} 1 2 3 4 5 6 7 8 9 {10}
------------------------------------
4. Apache commons-lang3
对于Apache commons-lang3库,我们可以使用StringUtils.reverse
反转字符串和StringUtils.reverseDelimited
反转单词。
pom.xml
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.10</version>
</dependency>
import org.apache.commons.lang3.StringUtils;public class ReverseString3 {public static void main(String[] args) {System.out.println(StringUtils.reverse("Hello World Java")); // reverse stringSystem.out.println(StringUtils.reverseDelimited("Hello World Java", ' ')); // reverse words}
}
输出结果
avaJ dlroW olleHJava World Hello
查看其源代码,Apache-commons-lang3
其实是使用new StringBuilder(str).reverse()
来反转字符串。
package org.apache.commons.lang3;public class StringUtils {public static String reverse(final String str) {if (str == null) {return null;}return new StringBuilder(str).reverse().toString();}//...
}
欢迎关注我的博客,里面有很多精品合集
- 本文转载注明出处(必须带连接,不能只转文字):字母哥博客。
觉得对您有帮助的话,帮我点赞、分享!您的支持是我不竭的创作动力! 。另外,笔者最近一段时间输出了如下的精品内容,期待您的关注。
- 《手摸手教你学Spring Boot2.0》
- 《Spring Security-JWT-OAuth2一本通》
- 《实战前后端分离RBAC权限管理系统》
- 《实战SpringCloud微服务从青铜到王者》
- 《VUE深入浅出系列》
delphi64位 char数组转换string中文乱码_使用位运算、值交换等方式反转java字符串-共四种方法...相关推荐
- java 字符串 移位_使用位运算、值交换等方式反转java字符串-共四种方法
在本文中,我们将向您展示几种在Java中将String类型的字符串字母倒序的几种方法. StringBuilder(str).reverse() char[]循环与值交换 byte循环与值交换 apa ...
- ajax后台返回数据中文乱码_解决Jmeter返回值显示中文乱码 的三种方法及设置随机动态参数(非原创侵删)...
一.Jmeter返回值显示中文乱码解决方法 第一种解决方法: 1. 点击线程组--添加--后置处理器--BeanShell后置处理程序 2.点击BeanShell后置处理程序,编写:prev.setD ...
- 数组反转(倒叙)输出四种方法
public class ArrayDemo3 { public static void main(String[] args) {//思路1.数组倒序输出/*int[] nums = {3, 1, ...
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换
wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString.... #include <strin ...
- 怎么把char数组转换成String
调用reverseStr()传入一个字符串"let's" public String reverseStr(String s) {int left = 0, right = s.l ...
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system(command)...
转载:http://blog.csdn.net/chocolateconanlan/article/details/4058868 wchar_t*,wchar_t,wchat_t数组,char,ch ...
- char数组转换成字符串_将字符串转换为char数组java –将字符串转换为char
char数组转换成字符串 Sometimes we have to convert String to the character array in java programs or convert ...
- [C++]char转换为string ,固定长度的char数组转换为string
char转换为string 固定长度的char数组转换为string 使用socket编程时候,需要将受到的字符串数组转换为string,但是转换后的字符串总会附加一些,转义字符. 直接上code c ...
- 【从零开始学c++】4.char数组和string的千丝万缕
目录 1.char数组: char的赋值方法: char数组的赋值方法: ASCLL码: char类函数:需包含头文件 2.stirng: 3.对char数组的输入: 1.cin: 2.cin.etl ...
最新文章
- grafana官方使用文档_可视化监控展示工具之Grafana,安装部署和使用
- 状态模式在领域驱动设计中的使用
- ASP操作Excel技术总结
- bsgs(Baby Steps Giant Steps)算法
- .NET 高性能WEB架构-比较容易改造方式 - .NET架构
- shell 查看空行与删除空行
- 容器技术Docker K8s 7 容器服务ACK集群
- 编写可读代码的艺术读书整理
- C# WPF – 利用“Attached Property” 把 RoutedEvent 接上 ICommand
- 户外直播、移动直播遇到弱网信号差,如何保证流畅又高清画面传输?
- 【Tool工具】LICEcap 推荐 GIf录制工作- 如何简单录制一个gif(Mac Window都可)
- 分布式系统常见问题总结
- c++实现的阻塞队列
- 程序员,这12个问题让经理比你痛苦多了
- 2023哈尔滨工程大学计算机考研信息汇总
- JavaGUI——Java图形用户界面
- No Security No Safety!——基于Hypervisor技术的车联网安全网关基础平台
- 【Luat-air105】8.2 fatfs及照片存储
- 小学语文计算机的运用计划,语文计算机应用教学计划
- 《下一代互联网(IPv6)搭建与运维》