Browsing 15655 questions and answers with Jon Skeet

Java Regex to replace Octal value in string

I have set of octal values say (0177-0377). whenever these value I found in string, have to replace with ?. String a= "sccce¼»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕerferferfer"; for...
Jon Skeet
people
quotationmark

You don't want to do this to the whole file in one go - you need a streaming approach. I'd do something like this: // TODO: Rename to something more appropriate public static void replaceInvalidCharacters(Reader reader, Writer writer) { ... more 5/23/2017 1:33:50 PM

people

How to convert the Date format and store it in a Date field(not in string or println) with the new format in Java

I'm trying to get the date from an object and changing the format and down the line I need to use the date value as a date field itself(not as string). The format is getting...
Jon Skeet
people
quotationmark

A Date object doesn't have any state representing a format. It's just an instant in time. If you've got the right value in date1, then formatting and parsing it is completely pointless. Instead, when you want the Date in a particular... more 5/23/2017 12:25:55 PM

people

Json.Net deserialize JSON string inside a string

This sound very simple. But I can't find how to do it. I've receiving a bad json from an api. the actual json is inside a string Instead of...
Jon Skeet
people
quotationmark

If you've got a value which is "a string serialized as JSON", then just deserialize that first. Assuming your string genuinely starts and ends with a double quote, you should be fine to call JsonConvert.DeserializeObject<string> to... more 5/23/2017 11:01:22 AM

people

Escape XML using stringutils commons.lang version3 apply on writer

In the older version of "org.apache.commons.lang"(2.6) StringEscapeUtils, there is a method to escapexml by passing an instance of "java.io.Writer" as one of the parameter. eg:...
Jon Skeet
people
quotationmark

All the overloads accepting a Writer have been removed - but instead, CharSequenceTranslator instances are available, so you could write: StringEscapeUtils.ESCAPE_XML10.translate(input, writer); (Personally I'd strongly recommend using... more 5/23/2017 7:31:10 AM

people

C# Method reference of constructor

In C# I can have references of methods and static methods. Can I also get the reference of a classes constructor? In Java I can say Supplier<MyClass> createMyClass =...
Jon Skeet
people
quotationmark

No, there's no equivalent of method group conversions for constructors, or properties, indexers or operators. It's an entirely reasonable idea, but it isn't in C# at the moment. It is, however, tracked as part of a feature request in the... more 5/23/2017 6:42:46 AM

people

this is Sparta, or is it?

The following is an interview question. I came up with a solution, but I'm not sure why it works. Question: Without modifying the Sparta class, write some code that makes...
Jon Skeet
people
quotationmark

But why does Sparta in MakeItReturnFalse() refer to {namespace}.Place.Sparta instead of {namespace}.Sparta? Basically, because that's what the name lookup rules say. In the C# 5 specification, the relevant naming rules are in section... more 5/22/2017 8:02:07 PM

people

Partly negative period between 2 dates with NodaTime

I use NodaTime library to get period between two days, but sometimes I get partly negative period between two dates: var start = DateTime.Now; var end =...
Jon Skeet
people
quotationmark

TL;DR: Update to Noda Time 2.0.2. This was a bug in 2.0.0 and 2.0.1 of Noda Time. Brief testing suggests it wasn't an issue in earlier versions. The problem was due to an optimization when trying to obtain the time parts of a period... more 5/22/2017 4:26:34 PM

people

Java Static Initializer Block datatype

static int B,H; static boolean flag = true; static { Scanner scan = new Scanner (System.in); B = scan.nextInt(); H = scan.nextInt(); scan.close(); And...
Jon Skeet
people
quotationmark

Well in your second code, B and H are local variables within the block - you're not assigning to the field at all. That's exactly the same as if you were writing a method... Forget static initialization for the minute - imagine you had... more 5/22/2017 6:17:45 AM

people

How to create delegate for Result property getter in Task c#

I need to create a delegate for Result property of Task in c#( TResult getter_Result) I am able to get getter method for property Result Resultgetter =...
Jon Skeet
people
quotationmark

You can do this via expression trees, but the way your sample code is set up makes it look like you want just a Func<object>, which means the task has to be already embedded in the expression tree. You can do that: using... more 5/21/2017 6:56:12 PM

people

To Sum Value using linq

In the below code i have a list i am trying to get values from list using linq query and sum the values.But i don't know how to sum the values.So please help me to resolve the...
Jon Skeet
people
quotationmark

You shouldn't be including the value in your grouping - you want to group by just Desc and Month (I assume), then sum the value parts. Two options: var sums1 = objList .GroupBy(y => new { y.Desc, y.Month }) .Select(group =>... more 5/21/2017 6:20:01 PM

people