EzDevInfo.com

formatting interview questions

Top formatting frequently asked interview questions

How to escape braces (curly brackets) in a format string in .NET

How can brackets be escaped in using string.Format. For example:

String val = "1,2,3"
String.Format(" foo {{0}}", val); 

This example doesn't throw an exception, but outputs the string foo {0}

Is there a way to escape the brackets?


Source: (StackOverflow)

std::string formatting like sprintf

I have to format std::string with sprintf and send it into file stream. How can I do this?


Source: (StackOverflow)

Advertisements

The shortest possible output from git log containing author and date

How can I show a git log output with (at least) this information:

* author
* commit date
* change

I want it compressed to one line per log entry. What's the shortest possible format for that?

(tried --format=oneline but that does not show the date)


Source: (StackOverflow)

How can I format numbers as money in JavaScript?

I would like to format a price in JavaScript.
I'd like a function which takes a float as an argument and returns a string formatted like this:

"$ 2,500.00"

What's the best way to do this?


Source: (StackOverflow)

How do I change Eclipse to use spaces instead of tabs?

By default Eclipse indents with a hard tab character. How do I change it to spaces?


Source: (StackOverflow)

How to auto-format code in Eclipse?

How do you auto-format code in Eclipse?


Source: (StackOverflow)

Convert java.util.Date to String

I want to convert a java.util.Date object to a String in Java.

The format is 2010-05-30 22:15:52


Source: (StackOverflow)

How can I String.Format a TimeSpan object with a custom format in .NET?

What is the recommended way of formatting TimeSpan objects into a string with a custom format?


Source: (StackOverflow)

How to print a number with commas as thousands separators in JavaScript

I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this?

Here is how I am doing it:

function numberWithCommas(x) {
    x = x.toString();
    var pattern = /(-?\d+)(\d{3})/;
    while (pattern.test(x))
        x = x.replace(pattern, "$1,$2");
    return x;
}

Is there a simpler or more elegant way to do it? It would be nice if it works with floats also, but that is not necessary. It does not need to be locale-specific to decide between periods and commas.


Source: (StackOverflow)

Commonly accepted best practices around code organization in JavaScript [closed]

As JavaScript frameworks like jQuery make client side web applications richer and more functional, I've started to notice one problem...

How in the world do you keep this organized?

  • Put all your handlers in one spot and write functions for all the events?
  • Create function/classes to wrap all your functionality?
  • Write like crazy and just hope it works out for the best?
  • Give up and get a new career?

I mention jQuery, but it's really any JavaScript code in general. I'm finding that as lines upon lines begin to pile up, it gets harder to manage the script files or find what you are looking for. Quite possibly the biggest propblems I've found is there are so many ways to do the same thing, it's hard to know which one is the current commonly accepted best practice.

Are there any general recommendations on the best way to keep your .js files as nice and neat as the rest of your application? Or is this just a matter of IDE? Is there a better option out there?


EDIT

This question was intended to be more about code organization and not file organization. There has been some really good examples of merging files or splitting content around.

My question is: what is the current commonly accepted best practice way to organize your actual code? What is your way, or even a recommended way to interact with page elements and create reuseable code that doesn't conflict with each other?

Some people have listed namespaces which is a good idea. What are some other ways, more specifically dealing with elements on the page and keeping the code organized and neat?


Source: (StackOverflow)

Change date format in a Java string

I've a String representing a date.

String date_s = "2011-01-18 00:00:00.0";

I'd like to convert it to a Date and output it in YYYY-MM-DD format.

2011-01-18

How can I achieve this?


Okay, based on the answers I retrieved below, here's something I've tried:

String date_s = " 2011-01-18 00:00:00.0"; 
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); 
Date date = dt.parse(date_s); 
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));

But it outputs 02011-00-1 instead of the desired 2011-01-18. What am I doing wrong?


Source: (StackOverflow)

Why does the Visual Studio editor show dots in blank spaces?

I have a strange bug in the Visual Studio text editor. All my blank spaces are replaced by a "."

public class Person
{
  int age;
}

looks like this

public..class..Person..........................
{..................
..int age;...................
}.....................

I reset the settings to default. Didn't work. I also re-installed VS2008. Still didn't work. What's the error?


Source: (StackOverflow)

Formatting code snippets for blogging on Blogger [closed]

My blog is hosted on Blogger and I frequently post code snippets in C / C# / Java / XML etc. but I find the snippet gets "mangled".

Are there any web sites that I could use to parse the snippet beforehand and sort out the formatting, convert XML "<" to "&lt;" etc.

There are a numbers of questions around this area on SO but I couldn't find any that address this question directly.

Edit: For @Rich answer, site states "To display the formatted code on your site, you need to get this CSS stylesheet, and add a reference to it in the <head> section of your page". That's the problem - you can't do this on Blogger AFAIK.


Source: (StackOverflow)

Make a float only show two decimal places

I have the value 25.00 in a float, but when I print it on screen it is 25.0000000.
How can I display the value with only two decimal places?


Source: (StackOverflow)

C# convert int to string with padding zeros?

In C# I have an integer value which need to be convereted to string but it needs to add zeros before:

For Example:

int i = 1;

When I convert it to string it needs to become 0001

I need to know the syntax in C#.


Source: (StackOverflow)