The Source for Java Technology Collaboration

Home » java.net Forums » JDK » Java SE

Thread: Better String manipulation methods needed

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 16 - Last Post: Aug 21, 2006 12:20 PM by: jwenting
disciple285

Posts: 14
Better String manipulation methods needed
Posted: Aug 18, 2006 12:54 AM
  Click to reply to this thread Reply

I have mentioned this once somewhere else..

Why is String class missing so many helpful methods.
So that I have to import the jakarta commons stringUtils library??

This is so stupid, C# has methods in the string class lacking in Java. Java has essentials like substringBetween(vdvd,adsvfd) and others missing!

But then they have Regex expressions in string methods..but not simple methods to do things.

My reccomendation: put most of StringUtils into the string class and save developers from importing a library or being frustrated.

ilazarte

Posts: 26
Re: Better String manipulation methods needed
Posted: Aug 18, 2006 10:24 AM   in response to: disciple285
  Click to reply to this thread Reply

exactly what signatures and methods are you missing?
post it here and maybe you'll get lucky with implementations being provided. This post made the java.net homepage, so you have some visibility.

leouser

Posts: 1,160
Re: Better String manipulation methods needed
Posted: Aug 18, 2006 11:02 AM   in response to: ilazarte
  Click to reply to this thread Reply

better yet, implement the missing methods and submit them as a collaboration patch.

leouser

disciple285

Posts: 14
Re: Better String manipulation methods needed
Posted: Aug 21, 2006 10:25 AM   in response to: leouser
  Click to reply to this thread Reply

How do I submit a collaboration patch?

leouser

Posts: 1,160
Re: Better String manipulation methods needed
Posted: Aug 21, 2006 11:14 AM   in response to: disciple285
  Click to reply to this thread Reply

https://jdk-collaboration.dev.java.net/

leouser

kdt

Posts: 3
Re: Better String manipulation methods needed
Posted: Aug 18, 2006 1:52 PM   in response to: ilazarte
  Click to reply to this thread Reply

Here are some missing String functions:

boolean containsIgnoreCase(String)
static boolean isNullOrEmpty(String)

and more :)
http://doc.trolltech.com/4.1/qstring.html

fuerte

Posts: 129
Re: Better String manipulation methods needed
Posted: Aug 18, 2006 4:37 PM   in response to: ilazarte
  Click to reply to this thread Reply

String whatIwant = @"Finally I would like to have
   multi-line verbatim string literals for embedded
   SQL and HTML where I do not have to escape anything
   like ' and " and \ but instead everything is at it is.
   This string literal is terminated like this:"@;


leouser

Posts: 1,160
Re: Better String manipulation methods needed
Posted: Aug 19, 2006 1:03 PM   in response to: fuerte
  Click to reply to this thread Reply

'String whatIwant = @"Finally I would like to have
multi-line verbatim string literals for embedded
SQL and HTML where I do not have to escape anything
like ' and " and \ but instead everything is at it is.
This string literal is terminated like this:"@;'

this doesn't appear that hard to implement. I put together a 35 line patch for the compiler and submitted it to the collab project. It does seem to have a nice effect of getting rid of having to think about escapes and such. Im skeptical it will make it in given that its a change to the language but it certainly appears doable without a lot of pain involved.

leouser

fuerte

Posts: 129
Re: Better String manipulation methods needed
Posted: Aug 19, 2006 3:50 PM   in response to: leouser
  Click to reply to this thread Reply

> this doesn't appear that hard to implement. I put
> together a 35 line patch for the compiler and
> submitted it to the collab project. It does seem to
> have a nice effect of getting rid of having to think
> about escapes and such. Im skeptical it will make it
> in given that its a change to the language but it
> certainly appears doable without a lot of pain
> involved.
>
> leouser

Hehe, good job, fingers crossed.

leouser

Posts: 1,160
Re: Better String manipulation methods needed
Posted: Aug 19, 2006 4:23 PM   in response to: fuerte
  Click to reply to this thread Reply

yup,
/@ is the only escape sequence in it. Everything else is pure verbatim literal magic.
String s = @"///Guess what/?//n"@;
translates into:
"///Guess what/?//n"

I was surprised by the amount of votes the bug/rfe has against it. Id guess the patch will show up in the collab forum pretty soon, so if folks are interested they can patch and test it out.

leouser

leouser

Posts: 1,160
Re: Better String manipulation methods needed
Posted: Aug 19, 2006 4:27 PM   in response to: leouser
  Click to reply to this thread Reply

whoops, I should have typed \@ is the only escape sequence.

leouser

ahmetaa

Posts: 21
Re: Better String manipulation methods needed
Posted: Aug 19, 2006 12:22 PM   in response to: ilazarte
  Click to reply to this thread Reply

well, i am not sure if it should be part of String object or a helper class method, but:
boolean hasText() : false if String is null or contains only white spaces.

ahmetaa

Posts: 21
Re: Better String manipulation methods needed
Posted: Aug 19, 2006 12:26 PM   in response to: ahmetaa
  Click to reply to this thread Reply

sorry it sohuld have been :
"false if String is null, empty or contains only white spaces.

disciple285

Posts: 14
Re: Better String manipulation methods needed
Posted: Aug 21, 2006 9:18 AM   in response to: ilazarte
  Click to reply to this thread Reply

My wish list would include:

center(int size)

rightPad(int size) - Right pad a String with spaces rightPad(int size, char padChar)
rightPad(int size, String padStr)

leftPad.. (same as above)

stripEnd(String stripChars)
stripStart(String stripChars)
stripToEmpty(String str)
stripToNull(String str)

substring(int start)
substring(int start, int end)
substringAfter(String separator)
substringAfterLast(String separator)
substringBefore(String separator)
substringBeforeLast(String separator)
substringBetween(String tag)
substringBetween(String open, String close)

trimToEmpty()
trimToNull()

Note: in String Utils some are Null safe:
"
IsEmpty/IsBlank - checks if a String contains text
Trim/Strip - removes leading and trailing whitespace
Equals - compares two strings null-safe
IndexOf/LastIndexOf/Contains - null-safe index-of checks
IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
ContainsOnly/ContainsNone - does String contains only/none of these characters
Substring/Left/Right/Mid - null-safe substring extractions
SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
Split/Join - splits a String into an array of substrings and vice versa
Remove/Delete - removes part of a String
Replace/Overlay - Searches a String and replaces one String with another
Chomp/Chop - removes the last part of a String
LeftPad/RightPad/Center/Repeat - pads a String
UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
CountMatches - counts the number of occurrences of one String in another
IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
DefaultString - protects against a null input String
Reverse/ReverseDelimited - reverses a String
Abbreviate - abbreviates a string using ellipsis
Difference - compares two Strings and reports on their differences
LevensteinDistance - the number of changes needed to change one String into another "

The full list is at: http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html

skaffman

Posts: 15
Re: Better String manipulation methods needed
Posted: Aug 21, 2006 1:20 AM   in response to: disciple285
  Click to reply to this thread Reply

Whenever the discussion arises of "which open-source projects should be incorporated into Java SE", surely the obvious answer is the main jakarta Commons projects?

Never mind Javascript processors and relational databases - lets just have commons-lang, -io and -collections, thank you very much. I think I know which I'd use more often.

disciple285

Posts: 14
Re: Better String manipulation methods needed
Posted: Aug 21, 2006 9:21 AM   in response to: skaffman
  Click to reply to this thread Reply

I agree.. Javascript is nice... but good String methods trump all new technologies.

I just want a standard library that is already very usefull.
Will String be bloated? Maybe. But rather have lots of good methods than have to write an extra 3 or 4 lines of code to something oft-needed..

like subStringBetween(String s1, String s2)
Thank you.

jwenting

Posts: 478
Re: Better String manipulation methods needed
Posted: Aug 21, 2006 12:20 PM   in response to: skaffman
  Click to reply to this thread Reply

> Whenever the discussion arises of "which open-source
> projects should be incorporated into Java SE", surely
> the obvious answer is the main jakarta Commons
> projects?
>
no, the obvious answer is ALL OF THEM, never mind that they're often mutually exclusive (you just create another fork and call that Java as well).




 XML java.net RSS