Java实现一个字符串的反转
Java小程序实现字符串的反转:
方法一:
public class reverseString {
public static void main(String[] args) {
String str="ILoveJavaAndPython";
char[] strArray=str.toCharArray();
int len=strArray.length;
char temp;
for(int i=0;i<len/2;i++){
temp=strArray[i];
strArray[i]=strArray[len-i-1];
strArray[len-i-1]=temp;
}
String strAfter=String.valueOf(strArray);
System.out.println("Before:"+str);
System.out.println("After:"+strAfter); }
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARoAAAA3CAIAAABclBKxAAAGwUlEQVR4nO1cwZmrPAx0W5uCaGdTwl8FzeT0Lq+Ed+U/bEJsSSMJMCFsZr69LGBLsjSW7GDKNE1//v6b/ybiEzAO5ev7Zl0exgPUmabb95cpGl0/FGD0pmkqE+n0Ebh9f81hOQ7FjodxKKWUI+L3NHS6DxHp9Om4fX+VO0AwHIjT0MkH6UQQ3QDp9B9BEAvB7EQQ3UA6EUQ3kE4E0Q2kE0F0A+lEEN3wMjrdf/4qR/1OSLwC41DK5br7j1rj8KZBFNOp4UEpq4ZrHNa12wfKImHVQc66XS9Cp5fq8BiVTUIbOm2OnNv1Yjc6O52eqq9hxptaj7z1DurerpfX6jAOpQzXrVI1nUTkLAqdD6ATthHjRSXAUpBOT4xDKcO42XSPTo/8m+/+Q+lUZ3VkPaITXlPdrpdyud7qIkh0EskNUulqOtk628EiOwt1rjoTfbXV4DK5oG399DACdS/XW622Kckq6fQ4zv7wddZlr5CtH1RDCeMqYdF6LKWTHofmfhOh5hpFjff830/Xz3GZx+reuYh+LLdVDUftKjp5OhsN20sJnWvdRCjK5d18PyEXtp0v3Z8QU9/sQxHItZqioMvQyZrpdGj52Qlr5cZV0HYbtm5F2AldaGdnJ5TmmnBDeTElt392CnRW8hLFjyPIcXKriC83NEJpKSYtlV1rHqtwCOxt5p6qsTG0AZ2MJApaKR9BizZicbHXBjmYF42iTI2KNVbNNS+icnIDLKdTTud2XnQma0fncO3UNvXkRm3df5XSKjJRmgs3sQydreo2tXaqLkQ+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Wg3LnE/nXkRj46MZ3AqDiuF1j1SYbpnNlpqoZCHT93Dd5EJ99HqmrGdaUa87iteNbNTtBo0fT3Zidr+Owvg6xaOznWpujkrJ1Q8Bn91ppspdPjeZsf0CCplbVBgrTyfaR30SB11QyQaQtvBf4N5TqF4lnpZKutFI2Xr5bt/u8uGTrdp2VzZ88vjeSuEV7kGkOQULzoB/wQkKVP8FWg6kLko8bch3LG3L/0K0jTNLlfX8p8BwLJ9X10VjohraWm0USEf7rAC4q9spMWizd3zQuBRfO96GcV25i5v2bKdrRK+Ki21koGT521E4O21W0zANwFoyfX8xEob5u9WYHX0IpvlO+AxKxM/EqQTv1xQJFBvAdIp96wy0/iI0A6dcNcsrPK+1iQTgTRDaQTQXQD6UQQ3UA6EUQ3kE4E0Q2kE0F0w/8ILv4NWBI3PgAAAABJRU5ErkJggg==" alt="" />
方法二:
import java.util.Stack; public class reverseString {
public static void main(String[] args) {
String str="ILoveJavaAndPython";
char[] strArray=str.toCharArray();
int len=strArray.length; Stack<Character> stack=new Stack<Character>();
for(int i=0;i<len;i++)
stack.push(strArray[i]);
String afterReversed="";
for(int i=0;i<len;i++)
afterReversed+=stack.pop();
System.out.println("Before:"+str);
System.out.println("After:"+afterReversed); }
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAAA9CAIAAAD5+uHqAAAGp0lEQVR4nO1cUaKrOgjMtk4X5HZOl/BW4WZcTN/HbTUBBlCjrT0zf60mQGACicbyIL4V0+9PeeLnd3q3NgLT708Zxvz/n47ybgUI4huwEOk/giC2ghmJIDqARCKIDiCRCKIDSCSC6AASiSA64DgijUNZcMUnA0QC41DK7X74Q6px+PQgMojUMKCUTQM1DtvaHQNlkbDqTW6a7jeh06k6vEZll9CGSLsjZ7rf7EbXJdKi9BZOfKjdyE+foO50v52rwziUMtz3StVEEpGzKnS+mkjYOoyTEv5akEgLxqGUYdxtukekV87Nd//niFTncGQ3IhJeO033W7ndp7rkEZ1EcoP0uZlIts52mMjOQ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9uRSctgj4yX6JO6Oy0VcCfzVDuS8n1Vt0XJVLFE30G0oA+WGbbAw+3zL+rIbT6ASeO5BsoVlEl6KFtQq/sBweZ8KG6gEgyuOyRdvuyfPTvD3VKpD6Z6QZp9AUiYSw+spD6gkWrF/LRVYmkXApTgIX8OcfWE2rWgkwCJ47k3IYJWV822+pcmTipGMaO+jqCddI2YZF5N44gQyuZFVefKp6vOSrqs9VQbuCjixJJjKw10FuIBOakdhbaVnJ7h7c6nOnHHmqvWF8/QXnRTTGxVgkfgZ4tnZ3oc3r2zVWNHbnxtzGuSCQ5dCgxbCKSgaYUOIhIVnXeg0jq8DUs+WUi1SO4hkiRj/TqDuRNY3TctupGuSuJ/evIjXx0SSKB8XCcLrDp0wiPa2akRzUU6jC4a/AuIvk+UjUyriLVmMdtxb1uRoJGi6bfmJGsgbO/zbFpjeTYmSKSs0ZCYWf0W2uyl0iv+21mQIOkVtYWCNLK95HeIYOkVdzPtIWXAv+Gcp2y8HpEshVWKsYLVMtq/zlKhkjPqdjctfMLIbkjhJexxhAkFC/6Bt/5stAJvshT/RH5qDH3pZwx36/9AtHj8XC/fJT5HgOS6/voekRC+kodo8kHP4rAC4ejMpIWi7dszT8Ci+Zr0WMS25i5v2aadrRK+Ki21koAi87aiUHb6rIZAO7C0JPr+QgUs82+q8DJhOLb3z2QmImJ7waJ1AHvLCmIzwCJtBt2sUn8LZBI2zGX5qzpCBKJIDqARCKIDiCRCKIDSCSC6AASiSA6gEQiiA4gkQiiA0gkgugAEokgOoBEIogOIJEIogNIJILoABKJIDqARCKIDvgfr4Kw/ktQaxUAAAAASUVORK5CYII=" alt="" />
Java实现一个字符串的反转的更多相关文章
- Java判断一个字符串中有多少大写字母、小写字母和数字
Java判断一个字符串中有多少大写字母.小写字母和数字 思路: 大写字母就是A-Z之间,小写字母是a-z之间,数字就是0-9之间,于是做判断就好:用到的String知识点,遍历字符串, 长度方法len ...
- Java 获取一个字符串中,另一个字符串出现的次数
Java 获取一个字符串中,另一个字符串出现的次数 思想: 1. indexOf到字符串中到第一次出现的索引2. 找到的索引+被找字符串长度,截取字符串3. 计数器++ 代码实现: public cl ...
- java中一个字符串是另外一个字符串的字串
java中一个字符串是另外一个字符串的字串 String类中有一个方法 public boolean contains(Sting s)就是用来判断当前字符串是否含有参数指定的字符串例s1=“take ...
- Java判断一个字符串是否有中文
Java判断一个字符串是否有中文一般情况是利用Unicode编码(CJK统一汉字的编码区间:0x4e00–0x9fbb)的正则来做判断,但是其实这个区间来判断中文不是非常精确,因为有些中文的标点符号比 ...
- java输入一个字符串,打印出该字符串中字符的所有排列,随机打乱排序
import java.util.ArrayList;import java.util.Collections;import java.util.List; public class Test7{ ...
- JAVA 判断一个字符串是不是一个合法的日期格式
原文:http://www.cnblogs.com/xdp-gacl/p/3548307.html 最近开发公司的项目,一直找不到合适的正则表达式可以判断一个字符串是否可以转成日期,今天发现可以采用S ...
- java判断一个字符串是否包含某个字符
一.contains方法 1:描述 java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列 2:声明 public boolean cont ...
- Java判断一个字符串str不为空:方法及时间效率
判断一个字符串str不为空的方法有: 1.str == null; 2.”“.equals(str): 3.str.length <= 0; 4.str.isEmpty(): 注意:length ...
- java 判断一个字符串中的数字:是否为数字、是否包含数字、截取数字
题外话: JavaScript中判断一个字符是否为数字,用函数:isDigit(); 一.判断一个字符串是否都为数字 package com.cmc.util; import java.util.re ...
随机推荐
- python字符decode与encode的问题
同事在工作中遇到一个字符编码的问题:问题是:从mysql数据库中读出来的varchar类型数据在python是unicode类型的. 但他却对这个unicode字符进行了decode,因为他以为读出来 ...
- 2016 - 1 -17 GCD主队列与全局队列
一:主队列 1.概念:每一个应用程序对应唯一一个主队列,直接GET即可:在多线程开发中,使用主队列更新UI dispatch_queue_t q = dispatch_get_main_queue() ...
- OnePlus安装Kali-NetHunter
1.关于 Kali NetHunter Kali NetHunter 是一款由 Offensive Security 团队研发设计的,以 Nexus(手机/平板)为基本 硬件设备,基于原生 Andro ...
- 极客DIY:廉价电视棒玩转GNSS-SDR,实现GPS实时定位
0×00 前言 GNSS是Global Navigation Satellite System的缩写.中文称作:全球卫星导航系统.全球导航卫星系统. GNSS泛指所有的卫星导航系统,包括全球的.区域的 ...
- Python ~~~ 面向对象的利器
class Rectangle(): # 有没有括号都行 . def __init__(self,x,y): self.x=x self.y=y def getPeri(self): def getA ...
- HDOJ-三部曲一(搜索、数学)- A Knight's Journey
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- iOS:音频
ios中有很多支持音频的控件,如:播放本地音乐(file URL)的AVAudioPlayer和AudioToolbox.Framework.可以播放音乐库音乐的MPMusicPlayerContro ...
- mysql 日期 时间戳 转换
/***************************************************************************************** * mysql 日 ...
- JDBC存取二进制文件示例
PutFile.java import java.io.File; import java.io.FileInputStream; import java.sql.*; public class Pu ...
- ARM异常中断处理
ARM异常中断处理 在ARM体系中,通常有以下3种方式控制程序的执行流程: 在正常程序执行过程中,每执行一条ARM指令,程序计数器寄存器(PC)的值加4个字节:每执行一条Thumb指令,程序计数器寄存 ...