Asked  7 Months ago    Answers:  5   Viewed   40 times

In .NET, what is the difference between String.Empty and "", and are they interchangable, or is there some underlying reference or Localization issues around equality that String.Empty will ensure are not a problem?

 Answers

65

In .NET prior to version 2.0, "" creates an object while string.Empty creates no objectref, which makes string.Empty more efficient.

In version 2.0 and later of .NET, all occurrences of "" refer to the same string literal, which means "" is equivalent to .Empty, but still not as fast as .Length == 0.

.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.

See the .NET specification for more information.

Tuesday, June 1, 2021
 
etsous
answered 7 Months ago
12

new String("text"); explicitly creates a new and referentially distinct instance of a String object; String s = "text"; may reuse an instance from the string constant pool if one is available.

You very rarely would ever want to use the new String(anotherString) constructor. From the API:

String(String original) : Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since strings are immutable.

Related questions

  • Java Strings: “String s = new String(”silly“);”
  • Strings are objects in Java, so why don’t we use ‘new’ to create them?

What referential distinction means

Examine the following snippet:

    String s1 = "foobar";
    String s2 = "foobar";

    System.out.println(s1 == s2);      // true

    s2 = new String("foobar");
    System.out.println(s1 == s2);      // false
    System.out.println(s1.equals(s2)); // true

== on two reference types is a reference identity comparison. Two objects that are equals are not necessarily ==. It is usually wrong to use == on reference types; most of the time equals need to be used instead.

Nonetheless, if for whatever reason you need to create two equals but not == string, you can use the new String(anotherString) constructor. It needs to be said again, however, that this is very peculiar, and is rarely the intention.

References

  • JLS 15.21.3 Reference Equality Operators == and !=
  • class Object - boolean Object(equals)

Related issues

  • Java String.equals versus ==
  • How do I compare strings in Java?
Tuesday, June 1, 2021
 
StampyCode
answered 7 Months ago
32

There are two key differences:

First, attributes assigned to a view via style will apply only to that view, while attributes assigned to it via android:theme will apply to that view as well as all of its children. For example, consider this style resource:

<style name="my_background">
    <item name="android:background">@drawable/gradient</item>
</style>

If we apply it to a LinearLayout with three child TextViews by using style="@style/my_background", then the linearlayout will draw with a gradient background, but the backgrounds of the textviews will be unchanged.

If instead we apply it to the LinearLayout using android:theme="@style/my_background" then the linearlayout and each of the three textviews will all use the gradient for their background.

The second key difference is that some attributes only affect views if they are defined in that view's theme. For example, consider this style resource:

<style name="checkboxes">
    <item name="colorAccent">#caf</item>
    <item name="colorControlNormal">#caf</item>
</style>

If I apply this to a CheckBox using style="@style/checkboxes", nothing will happen. If instead I apply it using android:theme="@style/checkboxes", the color of the checkbox will change.

Just like the first rule said, styles containing theme attributes will apply to all children of the view with the android:theme attribute. So I can change the color of all checkboxes in a linearlayout by applying android:theme="@style/checkboxes" to my linearlayout.

Sunday, August 22, 2021
 
McAuley
answered 4 Months ago
97

Use a CSV parser like OpenCSV to take care of things like commas in quoted elements, values that span multiple lines etc. automatically. You can use the library to serialize your text back as CSV as well.

String str = "value1, value2, value3, value4, "value5, 1234", " +
        "value6, value7, "value8", value9, "value10, 123.23"";

CSVReader reader = new CSVReader(new StringReader(str));

String [] tokens;
while ((tokens = reader.readNext()) != null) {
    System.out.println(tokens[0]); // value1
    System.out.println(tokens[4]); // value5, 1234
    System.out.println(tokens[9]); // value10, 123.23
}
Monday, August 23, 2021
 
Simmeringc
answered 4 Months ago
24

When you execute the given ffmpeg command line, your shell parses it into a set of command line arguments that are essentially:

{
    "ffmpeg",
    "-i",
    "input.jpg",
    "-vf",
    "scale='if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'",
    "output_320x240_boxed.png",
}

The extra quotes in the scale=... argument interpreted by the shell, rather than being passed on to the underlying program. So when executing the same program with Go, where you are passing a list of arguments directly, you should leave out those extra quotes.

Wednesday, September 22, 2021
 
davidb
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share