java 将文本字符串中的字符移动指定数量的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27214783/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:24:52  来源:igfitidea点击:

moving the characters in a text string a specified number of positions

javastring

提问by andrey shirokij

I am new in programming and I am trying to write a program that moves the characters in a text string a specified number of positions.

我是编程新手,我正在尝试编写一个程序,将文本字符串中的字符移动指定数量的位置。

The program must include a method whose inputs will be a text string (type String) and the number of positions (type int). The output will be a string with characters shifted.

该程序必须包含一个方法,其输入将是一个文本字符串(类型 String)和位置数(类型int)。输出将是一个字符移位的字符串。

For example, moving 4 positions:

例如,移动 4 个位置:

rabbit eats a carrot
it eats a carrotrabb

Now I have this partial code. I can erase first characters but I don't know how to put them to the end of this text. How can i make it?

现在我有了这个部分代码。我可以删除第一个字符,但我不知道如何将它们放在本文的末尾。我怎样才能做到?

public static void main(String[] args) {
    System.out.println("enter the text: ");
    Scanner cti = new Scanner(System.in);     
    String a = cti.nextLine();
    System.out.println("enter number of positions= ");
    int b = cti.nextInt();
    char firstLetter = a.charAt(0);
    b--;
    a = a.substring(b); 
    String m = a + firstLetter ;
    System.out.println("now it is "+ m);
}

采纳答案by bsoren

import java.util.*;
public class JavaApplication5 {
    public static void main(String[] args) {
        System.out.println("enter the text: ");
       Scanner cti = new Scanner(System.in);     
       String a = cti.nextLine();
        System.out.println("enter number of positions= ");
        int b = cti.nextInt();
       String firstPart = a.substring(0,b);   // line 1
       b--;
       a = a.substring(b); 
       String m = a + firstPart ;             // line 2
        System.out.println("now it is "+ m);
    }
    
}

See the changes above in statement marked with comment line 1 and line 2.

请参阅上面用注释第 1 行和第 2 行标记的语句中的更改。

In line 1, we are getting the first part of string and in line 2, adding at the end of second string part.

在第 1 行,我们得到字符串的第一部分,在第 2 行,在第二个字符串部分的末尾添加。

回答by sasuke

public String foo(String s, int n) {
    String s2 = s.substring(0, n);
    s = s.substring(n) + s2;
    return s;
}

you can put a few validations on this, like null string or n is less than s.length() etc.

您可以对此进行一些验证,例如空字符串或 n 小于 s.length() 等。

回答by Bohemian

If you use regex, it's just one line:

如果您使用正则表达式,它只是一行:

return str.replaceAll("^(.{" + n + "})(.*)", "");

回答by dnsh

It is better to use modulus operator to calculate number of shifts. When initial number of shift is more than string length. Check this :

最好使用模数运算符来计算移位次数。当初始移位次数大于字符串长度时。检查这个:

public String shift(String string,int n){
    int nshift = string.length() < n ? n%string.length() : n ;
    String a = string.substring(0,nshift);
    return string.substring(nshift) + a ;
}

回答by striving_coder

One more version. All the work is essentially done in 1 line here:

再来一个版本。所有的工作基本上都是在这里的 1 行中完成的:

String result = new StringBuilder(a).delete(0, b).append(a.substring(0,b)).toString();

Anyway, the full code is:

无论如何,完整的代码是:

import java.util.*;
public class ShiftLetters {
    public static void main(String[] args) {
        System.out.print("enter the text: ");
        Scanner cti = new Scanner(System.in);     
        String a = cti.nextLine();
        System.out.print("Enter number of positions: ");
        int b = cti.nextInt();

        String result = new StringBuilder(a).delete(0, b).append(a.substring(0,b)).toString();

        System.out.println(result);
    }    
}

Also, you might want to be more accurate with your indentation style to improve readability.

此外,您可能希望缩进样式更准确以提高可读性。