这是网上看到的一篇java面试题中的问题:

  问题是: 如何将一个String字符串反转。

  

        String str = "1234567";
int length = str.length();
int beginIndex = length-1;
char[] sourceCharArray = str.toCharArray();
char[] discCharArray = new char[length];
int j=0;
for(int i=beginIndex; i>=0; i--){
discCharArray[j] = sourceCharArray[i];
j++;
}
String newStr = String.valueOf(discCharArray);  

由此推出来的操作是 字符串的某个字符的操作。可以直接先转化成char[],之后,在对某个指针下的char操作,在通过String.valueOf(char[])重新拼装成String字符串。效率更高。

关于String字符串反转的更多相关文章

  1. String字符串反转

    new StringBuffer("abcde").reverse().toString(); 通过char数组进行转换 package com.test.reverse; pub ...

  2. [C/C++] String Reverse 字符串 反转

    #include <iostream> #include <string> #include <algorithm> #include <cstring> ...

  3. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

  4. Java算法之字符串反转分析

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 在基本的工作内容开发中,算法不会显得那么重要,而在百万级别的时候,差距非常大,今天带大家研究下常见的 ...

  5. 字符串反转C#的实现

    字符串反转是面试过程中出现频率较高的算法题,今天一个牛同事让我用C#帮他实现这个算法,前提当然是不能使用类库. 例如: how are you 的反转结果为 you are how. 算法1: 是我当 ...

  6. 趣味算法:字符串反转的N种方法(转)

    老赵在反对北大青鸟的随笔中提到了数组反转.这的确是一道非常基础的算法题,然而也是一道很不平常的算法题(也许所有的算法深究下去都会很不平常).因为我写着写着,就写出来8种方法……现在我们以字符串的反转为 ...

  7. Java实现字符串反转的8种方法

    /** * */ package com.wsheng.aggregator.algorithm.string; import java.util.Stack; /** * 8 种字符串反转的方法, ...

  8. Java实现字符串反转

    替换原则:index k 的值和 n-k 的值进行交换.(始终记住程序员的n.k都是字符串的实际位置.) 乘除的最基本实现还是来源于移位操作. public String reverse(String ...

  9. java几种字符串反转

    java实现的字符串翻转,能想到的这几种方法 假设有其它方法,欢迎交流 //字符串反转 public class ReverseString { public String reverse1(Stri ...

随机推荐

  1. Windows - 程序猿应该熟记的CMD常用命令

    notepad 计事本 mspaint 画图 iisreset 重启IIS appwiz.cpl 控制面板 inetmgr IIS管理器 eventvwr 事件查看器 mstsc 远程桌面 net s ...

  2. PHP学习笔记三十一【const】

    <?php //常量都是public类型 // const 常量名=赋值 .变量名不需要加$符号,也不需要要访问修饰符,默认就是public class A{ const TAX_RATE=0. ...

  3. QT TCP/IP

    QT 网络通信(TCP/IP) 服务端: 一.监听新的客户端接入(QTcpServer) 重写函数 incomingConnection(qintptr socketDescriptor) 二.服务端 ...

  4. Linux下VMWare虚拟机的使用技巧

    使用技巧: 1.虚拟机安装文件:vm-workstation-full-8.0.3-703057.x86_64.bundle,./vm-workstation-full-8.0.3-703057.x8 ...

  5. js post传值

    一种是ajax传值,另一种是post传值, ajax传值: $.ajax({ url: "AjaxTxt/Fild.ashx?Name=duibi&dates=" suzk ...

  6. 创建兼容的XHR对象

    function createXHR () {//创建XMLHttpRequest对象 var xhr=null; if(window.XMLHttpRequest){ createXHR=creat ...

  7. Javascript经典实例 - 字符串

    1] 'this is a string'这是字符串直接量,new String('this is a string')这是字符串对象,字符串对象可以用字符串对象所带的属性和方法,直接量在“表面上”也 ...

  8. C#代码实现,确保windows程序只有一个实例(instance)

    static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...

  9. mysql不能链接远程,报(Host '***.***.***.***' is not allowed to connect to this MySQL server)

    Host '***.***.***.***' is not allowed to connect to this MySQL server 其中***...是本机公网ip; 解决办法: 首先看报错窗口 ...

  10. android android:textColor="@[package:]color/filename" ----Color/filename文字颜色selector状态列表

    文字颜色selector状态列表