Java 在字符串中移动字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20252195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shifting characters within a string
提问by theGreenCabbage
String newStr;
public RandomCuriosity(String input){
newStr = input;
}
public void shiftChars(){
char[] oldChar = newStr.toCharArray();
char[] newChar = new char[oldChar.length];
newChar[0] = oldChar[oldChar.length-1];
for(int i = 1; i < oldChar.length; i++){
newChar[i] = oldChar[i-1];
}
newStr = String.valueOf(newChar);
}
I created a method that shifts characters forward by one. For example, the input could be:
我创建了一种将字符向前移动一个的方法。例如,输入可以是:
The input: Stackoverflow
输入: Stackoverflow
The output: wStackoverflo
输出: wStackoverflo
How I did it is I mutated an instance of a string. Convert that string to a char
array (calling it oldChar
), assigned the last index of of oldChar
as the first index of newChar
, and made a for-loop that took the first index of oldChar
as the second index of my new Char
array and so forth. Lastly, I converted the char array back to a string.
我是怎么做的,我改变了一个字符串的实例。将该字符串转换为char
数组(称为oldChar
),将 of 的最后一个索引指定oldChar
为 的第一个索引newChar
,并创建一个 for 循环,将 的第一个索引oldChar
作为我的新Char
数组的第二个索引,依此类推。最后,我将字符数组转换回字符串。
I feel like I did way too much to do something verysimple. Is there a more efficient way to do something like this?
我觉得我做得太多了,无法做一些非常简单的事情。有没有更有效的方法来做这样的事情?
EDIT Thanks for the great answers!
编辑感谢您的出色回答!
采纳答案by JB Nizet
newStr = newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length() - 1);
回答by Alexis C.
You can made your life simpler :
你可以让你的生活更简单:
public static void main (String[] args) throws java.lang.Exception {
String input = "Stackoverflow";
for(int i = 0; i < s.length(); i++){
input = shift(input);
System.out.println(input);
}
}
public static String shift(String s) {
return s.charAt(s.length()-1)+s.substring(0, s.length()-1);
}
Output :
输出 :
wStackoverflo
owStackoverfl
lowStackoverf
flowStackover
rflowStackove
erflowStackov
verflowStacko
overflowStack
koverflowStac
ckoverflowSta
ackoverflowSt
tackoverflowS
Stackoverflow
回答by quazzieclodo
You could use System.arrayCopy:
您可以使用System.arrayCopy:
char[] oldChar = newStr.toCharArray();
char[] newChar = new char[oldChar.length];
newChar[0] = oldChar[oldChar.length - 1];
System.arrayCopy(oldChar, 0, newChar, 1, oldChar.length - 1);
回答by T0to
You can use StringBuilders.
您可以使用 StringBuilder。
StringBuilder strb = new StringBuilder();
strb.append(oldChar[oldChar.length-1]).append(oldchar.substring(0, oldChar.length-1));
newStr = strb.toString();
回答by subash
try this..
尝试这个..
String old = "String";
char first = old.charAt(old.length()-1);
String newString = first+old.substring(0,old.length()-1);
System.out.println(newString);
回答by Omar Abusabha
By Java, u can shift it to forward by O(n) where n is how many times to go forward by character which space o(1)
通过 Java,您可以将其向前移动 O(n),其中 n 是按字符哪个空格 o(1) 向前移动多少次
public static String shiftChars(String s , int times) {
String temp = s;
for (int i = 0; i < times ; i++) {
temp = temp.charAt(temp.length()-1)+temp.substring(0, temp.length()-1);
}
return temp;
}