2

I'm trying split a string when ever a " " occurs, for example the sentence 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?

2
  • 1
    Two points. First, note that the .substring(...) method that takes just one parameter works better here. Second, are you sure you only want to split on a single space? What about multiple spaces? What about tabs, new lines. What about sentences that end in punctuation, eg "this is a sentence." -- do you want the last string to include the '.'? All of these should be factored in when using the String.split(...) operation; see @Etaoin's answer for how it can be used. Commented May 16, 2010 at 23:13
  • Thanks for all the responce on the question. I should add im quite a novice at Java only been trying 2 learn it for 2 weeks now. What im trying to do is a simple translater that takes every word that is writen. moves the first letter to the back of the word and adds ojoj after and then displays it. An example would be the text "Test abc" is enterd and it displays "esttojoj cbaojoj". i will analyse all the anwsers i got here and se if i can get it to work. Thanks again for all the help. Commented May 16, 2010 at 23:38

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to split-tranform-join for this; replaceAll can do this in one step.

    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:

\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.


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. Note that for example, the above regex transforms text: to extt: (i.e. the colon is kept where it is).

The following variation splits on whitespaces \s, and reorders the head/tail of any sequence of non-whitespace characters \S. This should be identical to your current split(" ")-transform-join solution:

    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. 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

Comments

0

Use String.split to break the string apart. Then, run your code on each part. You can put the string together again using StringBuilder and a loop.

1 Comment

For a more general transformation, you have to do this, but this particular transformation is simple enough that you can do this in one step. See my replaceAll answer.
0

If performance is an issue, consider using StringTokenizer instead of split, StringTokenizer is much faster.

2 Comments

StringTokenizer is legacy class, much better replacement exist. Read the API. Even the 1.4.2 version you linked says it's legacy ("its use is discouraged in new code").
So what is the replacement then? I have heard the same argument before, but on two occasions that I can remember clearly (last one a few months ago) when we switched from split to tokenizer, did we speed up performance with some 40%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.