11package com .diffblue .javatest ;
22
3+ import java .io .PrintStream ;
34import java .util .Locale ;
45
56public class StringStuff {
67
8+ public static String classGetNameExample (Class c ) {
9+ StringStuff t = new StringStuff ();
10+ Class testClass = t .getClass ();
11+ if (c .getName ().equals (testClass .getName ()))
12+ return c .getName ();
13+ return "X" ;
14+ }
15+
16+ public static String classGetSimpleNameExample (Class c ) {
17+ StringStuff t = new StringStuff ();
18+ Class testClass = t .getClass ();
19+ if (c .getName ().equals (testClass .getName ()))
20+ return c .getSimpleName ();
21+ return "X" ;
22+ }
23+
24+ public static int integerParseIntExample (String s ) {
25+ int i = Integer .parseInt (s );
26+ if (i == 4 )
27+ return i ;
28+ else if (i == -10 )
29+ return i ;
30+ else
31+ return i ;
32+ }
33+
34+ public static String objectToStringExample (int i ) {
35+ Object obj ;
36+ if (i == 1 )
37+ obj = new Integer (1 );
38+ else if (i == 2 )
39+ obj = new String ("2" );
40+ else
41+ obj = new StringBuilder ("3" );
42+ return obj .toString ();
43+ }
44+
45+ public static void printStreamExample (String s ) {
46+ PrintStream ps = new PrintStream (System .out );
47+ ps .println (s );
48+ }
49+
50+ public static StringBuffer stringBufferAppendExample (String s ) {
51+ StringBuffer sb = new StringBuffer ();
52+ sb .append (s );
53+ sb .append ("=" );
54+ sb .append (s );
55+ return sb ;
56+ }
57+
58+ public static StringBuilder stringBuilderAppendExample (String s ) {
59+ StringBuilder sb = new StringBuilder ();
60+ sb .append (s );
61+ sb .append ("=" );
62+ sb .append (s );
63+ return sb ;
64+ }
65+
66+ public static int stringBuilderAppend2Example (StringBuilder sb ) {
67+ sb .append ("!" );
68+ int size = sb .toString ().length ();
69+
70+ if (size >= 3 ) {
71+ return 3 ;
72+ } else
73+ return 0 ;
74+ }
75+
76+ public static String stringBuilderAppend3Example (String s ) {
77+ StringBuilder sb = new StringBuilder ();
78+ sb .append (s );
79+ sb .append ("=" );
80+ sb .append (s );
81+ String ret = sb .toString ();
82+ return ret ;
83+ }
84+
85+ public static String stringBuilderAppendCExample (char c ) {
86+ StringBuilder sb = new StringBuilder ();
87+ sb .append (c );
88+ sb .append ('=' );
89+ sb .append (c );
90+ return sb .toString ();
91+ }
92+
93+ public static String stringBuilderAppendDExample (double i ) {
94+ StringBuilder sb = new StringBuilder ();
95+ sb .append (i );
96+ sb .append ("=" );
97+ sb .append (i );
98+ return sb .toString ();
99+ }
100+
101+ public static String stringBuilderAppendFExample (float f ) {
102+ StringBuilder sb = new StringBuilder ();
103+ sb .append ("XYZ" );
104+ sb .append (f );
105+ return sb .toString ();
106+ }
107+
108+ public static String stringBuilderAppendIExample (int i ) {
109+ StringBuilder sb = new StringBuilder ();
110+ sb .append (i );
111+ sb .append ("=" );
112+ sb .append (i );
113+ return sb .toString ();
114+ }
115+
116+ public static String stringBuilderAppendJExample (long l ) {
117+ StringBuilder sb = new StringBuilder ();
118+ sb .append ("x" );
119+ sb .append (l );
120+ return sb .toString ();
121+ }
122+
123+ public static int stringBuilderAppendJ2Example (long l ) {
124+ StringBuilder sb = new StringBuilder ();
125+ sb .append (l );
126+ if (l <= 1000L )
127+ return 2 ;
128+ else
129+ return 1 ;
130+ }
131+
132+ public static String stringBuilderAppendObjectExample (Object obj ) {
133+ StringBuilder sb = new StringBuilder ();
134+ sb .append (obj );
135+ sb .append ("=" );
136+ sb .append (obj );
137+ return sb .toString ();
138+ }
139+
7140 public static StringBuilder stringBuilderAppendZExample (Boolean x ) {
8141 StringBuilder sb = new StringBuilder ();
9142 if (x )
@@ -106,4 +239,108 @@ else if (i > 0)
106239 else
107240 return -1 ;
108241 }
242+ // Test name
243+
244+ public static int stringLengthExample (String s ) {
245+ if (s .length () == 2 )
246+ return 2 ;
247+ if (s .length () == 4 )
248+ return 4 ;
249+ return s .length () + 1 ;
250+ }
251+ // Test name
252+
253+ public static int stringStartsWithExample (String s ) {
254+ if (s .startsWith ("hel" )) {
255+ return 1 ;
256+ }
257+ return 2 ;
258+ }
259+ // Test name
260+
261+ public static int stringStartsWith2Example (String s ) {
262+ if (s .startsWith ("hello" )) {
263+ if (s .length () == 10 )
264+ return 0 ;
265+ else
266+ return 1 ;
267+ }
268+ if (s .startsWith ("hel" ))
269+ return 2 ;
270+ else
271+ return 3 ;
272+ }
273+ // Test name
274+
275+ public static String stringSubstringIIExample (String s ) {
276+ if (s .length () <= 10 )
277+ return s ;
278+ else
279+ return s .substring (2 , 5 );
280+ }
281+ // Test name
282+
283+ public static char [] stringToCharArrayExample (String s ) {
284+ assert (s .length () >= 2 );
285+ return s .toCharArray ();
286+ }
287+ // Test name
288+
289+ static int stringToLowerCaseExample (String s ) {
290+ if (s .equals ("abc" ))
291+ return 0 ;
292+
293+ String a = s .toLowerCase ();
294+ if (a .equals ("abc" ))
295+ return 1 ;
296+
297+ return 2 ;
298+ }
299+ // Test name
300+
301+ public static String stringTrimExample (String s ) {
302+ String t = s .trim ();
303+ if (t .length () == s .length () - 3 )
304+ return t ;
305+ else
306+ return s ;
307+ }
308+
309+ public static int checkHtml (String s ) {
310+ int p = s .indexOf ('<' );
311+ if (s .length () > 30 )
312+ return -1 ; // we want a short example
313+
314+ if (p == -1 )
315+ return 0 ;
316+
317+ if (!s .startsWith ("<html>" , p ))
318+ return 0 ;
319+
320+ int q = s .lastIndexOf ('<' );
321+
322+ if (q == -1 )
323+ return 0 ;
324+
325+ if (s .startsWith ("</html>" , q )) {
326+ assert (false );
327+ return 1 ;
328+ } else {
329+ return 0 ;}
330+ }
331+
332+ public static int checkHtml2 (String s ) {
333+ if (s .length () > 30 )
334+ return -1 ; // we want a short example
335+
336+ String t = s .trim ();
337+
338+ if (!t .startsWith ("<html>" ))
339+ return 0 ;
340+
341+ if (!t .endsWith ("</html>" ))
342+ return 0 ;
343+
344+ return 1 ;
345+ }
109346}
0 commit comments