Reverse a Given String StringTokenizer

xiaoxiao2021-03-06  60

Time: 2004-9-08 23:16:27 Title:

TO Feng Dance Phoenix: There is no exaggeration, if you use your way, the interval symbols such as the comma are removed, but there is no Xiangzhuang watch to be printed. To Sankxuan: used StringTokenizer (A, Separators, True) The three-parameter constructor, beautiful, is indeed the best implementation. -------------------------------------------------- ---- Reconstruction, the following is the reconstructed code and execution results:

Java code:

Import Java.

Util.

StringTokenizer;

public

Class test

{

public

Static

Final

String separators = ", / t:

'';?! ";

public

Static

String Reverse

(

String Input

)

{

StringTokenizer ST =

New

StringTokenizer

(Input, Separators,

True

);

StringBuffer Words =

New

StringBuffer

("" "

);

While

ST.

HasmoreToKens

(

)

)

{

Words.

insert

(

0, ST.

NextToken

(

)

);

}

Return Words.

Tostring

(

);

}

public

Void Testreverse

(

)

{

String

[

] Sentences =

New

String

[

]

{

"Hello, World!",

"I am a student",

"AM I A Student? Yes, or no",

"AM I A Student? Yes, or no",

Zhuang Says:

'It's just a coding game.

'"}; For (INT i = 0; I

-------------------------------------------------- ------------------------ Results of the:

Quote:> Ant EXEC BUILDFILE: Build.xml Exec: [Java] SENTENCE [0] = [Hello, World!], After REVERSED: [! World, hello] [java] Sentence [1] = [i am a student] , After Reverse: [Student A am i] ​​[java] SENTENCE [2] = [AM I A Student? Yes, or no], after, yes? Student a i am] [java] Sentence [3 ] = [AM I A Student? Yes, or no], after: [NO or, YES? Student a i am] [Java] SENTENCE [4] = [Zhuang Says: 'It's Just a Coding Game.'], After Reverse: ['Game. Coding a Just S'IT': Says Zhuang] Learned, decided to answer this next time.

Java code:

Import Java.

Util.

StringTokenizer;

public

Final

Class StringReverseer

{

public

Final

Static

String default_deli = ", / t:

'';?! ";

public

Final

Static

Boolean Default_include_Delt =

True;

Private

StringTokenizer tokenizer =

NULL;

Public StringReverseer

(

String Source

)

{

THIS

(Source, Default_Deli, Default_include_Delt

);

}

Public StringReverseer

(

String Source,

String Delimiters

)

{

THIS

(Source, Delimiters, Default_include_Delt

);

}

Public StringReverseer

(

String Source,

String Delimiters,

Boolean Includedelimiter

)

{

Tokenizer =

New

StringTokenizer

(Source, Delimiters, IncludedEldelimiter

);

}

public

String Reverse

(

)

{

StringBuffer Result =

New

StringBuffer

(

);

While

(Tokenizer.

HasmoreToKens

(

)

)

{

RESULT.

insert

(

0, tokenizer.

NextToken

(

)

);

}

Return RESULT.

Tostring

(

);

}

}

Java code:

StringReverSer Reverseer =

New StringReverseer

("this is a test

String "

);

String reverse = Reverser.

Reverse

(

);

Time: 2004-9-13 13:59:35 Title:

Oh, it is just a different style of each person, and the idea is actually almost. Changed me, I will move tokenizer to Reverse (), because Tokenizer is only used in Reverse (), it is smaller than the encapsulation in the Class in the reverse (). / / -------------------------------------------------------------------------------------------- ---------------------------- // StringReverseWord.java

/ / -------------------------------------------------------------------------------------------- -----------------------------

Import java.util. *;

/ **

* ------------------------------------------------- ----------------------------

* Used to provide an esample of how to reverse all Words in a given string.

* ------------------------------------------------- ----------------------------

* /

Public class stringReverseWord {

Private static void dostringreverseword () {

String a = "alex michael hunter";

Stack stack = new stack ();

StringTokenizer TempstringTokenizer = New StringTokenizer (A);

While (TempstringTokenizer.hasmoreToKenS ()) {

Stack.push (TempstringTokenizer.nexTelement ());

}

System.out.println ("/ Noriginal String:" a);

System.out.print ("Reverse Word String:");

While (! stack.empty ()) {

System.out.print (stack.pop ());

System.out.print ("");

}

System.out.println ("/ n");

}

/ **

* Sole entry point to the class and application.

* @Param args array of string arguments.

* /

Public static void main (String [] args) {

DostringReverseWord ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-86603.html

New Post(0)