简体   繁体   English

Java分割字符串?

[英]Java splitting strings?

I'm trying split a string when ever a " " occurs, for example the sentence test abc. 每当出现“”时,我都会尝试拆分一个字符串,例如语句test abc。 Then move the first letter in each word from first to last. 然后将每个单词中的第一个字母从头到尾移动。 I got the moving the letter to work on the original string using 我将字母移动到使用

String text = JOptionPane.showInputDialog(null,"Skriv in en normal text:");
char firstLetter = text.charAt(0);
normal = text.substring(1,text.length()+0) + firstLetter;

So my question is how would I split the string then start moving the letters around in each part of the cut string? 所以我的问题是,我将如何分割字符串,然后开始在分割字符串的每个部分中移动字母?

Store your split strings in an array, then loop over the array and replace each one: 将拆分后的字符串存储在一个数组中,然后遍历该数组并替换每个字符串:

String[] pieces = originalString.split(" ");
for (int i = 0; i < pieces.length; i++)
    pieces[i] = pieces[i].subString(1) + pieces[i].charAt(0);

By the way, this will just get you started -- it won't correctly handle cases where there's more than one space, single-letter words, or any other special cases (because you didn't say what you wanted to do). 顺便说一句,这只会让您起步-无法正确处理存在多个空格,单字母单词或任何其他特殊情况的情况(因为您没有说想做的事情)。 You'll have to handle those yourself. 您必须自己处理。

You don't have to split -tranform-join for this; 您不必为此split -tranform-join; replaceAll can do this in one step. replaceAll可以一步完成。

    String text = "Skriv in en normal text:";
    text = text.replaceAll("(\\s*)(\\w)(\\w+)", "$1$3$2");
    System.out.println(text);
    // prints "krivS ni ne ormaln extt:"

Basically the regex captures 3 groups: 正则表达式基本上分为3组:

\1 : (\s*) : any optional preceding whitespace
\2 : (\w)  : the head portion of each "word"
\3 : (\w+) : any tail portion of each "word"

Then, as the replacement string makes it obvious and clear, you switch \\2 and \\3 around. 然后,随着替换字符串变得清晰明了,您可以切换\\2\\3


So it should be clear that replaceAll with capturing group is the best, most readable solution for this problem, but what that regex is depends on the problem specification. 因此,应该清楚的是,带有捕获组的replaceAll是解决此问题的最佳,最易读的解决方案,但是该正则表达式的内容取决于问题的规范。 Note that for example, the above regex transforms text: to extt: (ie the colon is kept where it is). 请注意,例如,上述正则表达式将text:转换为extt:即,将冒号保留在原处)。

The following variation splits on whitespaces \\s , and reorders the head/tail of any sequence of non-whitespace characters \\S . 以下变体在空白\\s上分割,并对非空白字符\\S的任何序列的头/尾重新排序。 This should be identical to your current split(" ") -transform-join solution: 这应该与您当前的split(" ") transform-join解决方案相同:

    String text = "bob: !@#$ +-";
    text = text.replaceAll("(\\s*)(\\S)(\\S+)", "$1$3$2");
    System.out.println(text);
    // prints "ob:b @#$! -+"

This variation do the switch on any word character \\w+ sequence surrounded by word boundary \\b . 此变体对由单词边界\\b包围的任何单词字符\\w+序列进行切换。 If this is what you need, then this is the simplest, most readable solution for the job. 如果这是您所需要的,那么这是最简单,最易读的解决方案。

    String text = "abc:def!ghi,jkl mno";
    text = text.replaceAll("\\b(\\w)(\\w+)\\b", "$2$1");
    System.out.println(text);
    // prints "bca:efd!hig,klj nom"

See also 也可以看看

Use String.split to break the string apart. 使用String.split将字符串分开。 Then, run your code on each part. 然后,在每个部分上运行您的代码。 You can put the string together again using StringBuilder and a loop. 您可以使用StringBuilder和循环将字符串再次放在一起。

如果性能是一个问题,请考虑使用StringTokenizer而不是split ,StringTokenizer快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM