Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalents of C# String.Format() and String.Join()

Tags:

java

string

c#

I know this is a bit of a newbie question, but are there equivalents to C#'s string operations in Java?

Specifically, I'm talking about String.Format and String.Join.

like image 306
Omar Kooheji Avatar asked Oct 09 '08 15:10

Omar Kooheji


People also ask

Is Java similar to C?

C is a procedural, low level, and compiled language. Java is an object-oriented, high level, and interpreted language. Java uses objects, while C uses functions. Java is easier to learn and use because it's high level, while C can do more and perform faster because it's closer to machine code.

What is C code in Java?

C is a Procedural Programming Language. Java is Object-Oriented language. C is more procedure-oriented. Java is more data-oriented. C is a middle-level language because binding of the gaps takes place between machine level language and high-level languages.

Is Java similar to C or C++?

A common misconception is that if a language is similar to another, then it must be similar in functionality. While Java and C++ are similar in syntax, they are far more dissimilar in the way they execute and process.

Is Java C based?

The syntax of Java is largely influenced by C++ and C. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language.


2 Answers

The Java String object has a format method (as of 1.5), but no join method.

To get a bunch of useful String utility methods not already included you could use org.apache.commons.lang.StringUtils.

like image 66
Grant Wagner Avatar answered Sep 22 '22 10:09

Grant Wagner


String.format. As for join, you need to write your own:

 static String join(Collection<?> s, String delimiter) {      StringBuilder builder = new StringBuilder();      Iterator<?> iter = s.iterator();      while (iter.hasNext()) {          builder.append(iter.next());          if (!iter.hasNext()) {            break;                            }          builder.append(delimiter);      }      return builder.toString();  } 

The above comes from http://snippets.dzone.com/posts/show/91

like image 26
Allain Lalonde Avatar answered Sep 24 '22 10:09

Allain Lalonde