string-formatting

Formatting a string using values from a generic list by LINQ

自闭症网瘾萝莉.ら 提交于 2020-01-04 09:28:28
问题 Question Updated I have a generic list which can contains the following values: Sector 1 Sector 2 Sector 4 or the following value: All Sectors I want to format this values in a string like this: Sector 1 & 2 & 4 - or All Sectors - Currently, I have the following code to format the same. It works, but is very complicated. string retrieveSectors += sectors.Count == 0 ? string.Empty : sectors.OrderBy( y => y.Sector.Substring( y.Sector.Length - 1, 1)). GroupBy(g => g.Sector).Select( g => g.First(

Reformat string to be comma separated and formatted

雨燕双飞 提交于 2020-01-04 06:15:45
问题 I have a list of strings... var strings = new List<String>() { "a", "b", "c" }; I want to output them in a different format, like this: 'a','b','c' I've tried : string.Join("','",strings ); and String.Join(",", String.Format("'{0}'",strings ) 回答1: Your first attempt should work, but you need to prefix and suffix the overall result with "'" . or, you could do: var strings = new List<string>() { "a", "b", "c" } .Select(x => string.Format("'{0}'", x)); var result = string.Join(",", strings);

Converting seconds to human readable format MM:SS Java [duplicate]

跟風遠走 提交于 2020-01-03 18:37:31
问题 This question already has answers here : Formatting seconds and minutes (4 answers) Closed 5 years ago . How can I convert a long int of seconds to the human readable format MM:SS Only SS should be 0 padded so long = 67 -> 1:07 回答1: String readable = String.format("%d:%02d", s/60, s%60); 回答2: Use integer division by 60 to turn seconds in to whole minutes. The Modulus(%) of seconds by 60 will give the "leftover" seconds. Then use a string conversion to check for the necessity of 0 padding. int

Converting seconds to human readable format MM:SS Java [duplicate]

孤者浪人 提交于 2020-01-03 18:36:40
问题 This question already has answers here : Formatting seconds and minutes (4 answers) Closed 5 years ago . How can I convert a long int of seconds to the human readable format MM:SS Only SS should be 0 padded so long = 67 -> 1:07 回答1: String readable = String.format("%d:%02d", s/60, s%60); 回答2: Use integer division by 60 to turn seconds in to whole minutes. The Modulus(%) of seconds by 60 will give the "leftover" seconds. Then use a string conversion to check for the necessity of 0 padding. int

TextBlock: Binding of Text and StringFormat

坚强是说给别人听的谎言 提交于 2020-01-03 17:17:33
问题 Is it possible to bind Text and StringFormat too? <TextBlock Text="{Binding Path=Price, StringFormat={Binding Path=DecimalPoints}}" /> DecimalPoints is constantly changing from F0 to F15 . Unfortunatelly the code above doesn't compile. 回答1: I think your best bet is definitely a converter. Then your binding would look like this: <TextBlock.Text> <MultiBinding Converter="{StaticResource StringFormatConverter }"> <Binding Path="Price"/> <Binding Path="DecimalPoints"/> </MultiBinding> </TextBlock

Print string left aligned with fixed width and suffix

与世无争的帅哥 提交于 2020-01-03 16:52:12
问题 Using Pythons string formatting, is there a nice way to add a suffix to a left aligned string that is padded to a fixed size? I want to print a list of key-value-pairs in the following formatting: a_key: 23 another_key: 42 ... The problem is the ':'. The best solution I found so far is to append the ':' to the key name: print "{:<20} {}".format(key+':', value) But I think this is a rather ugly solution, as it diminishes the separation of formatting and values. Is it possible to achieve this

Format string with multiple percent signs

痴心易碎 提交于 2020-01-03 13:59:29
问题 I know %% is used to escape actual % signs in a string, so %%%ds will end up with %10s in the following format string, but I don't know why I need %%5s in this string? After all, there are only two additional arguments (BUFFSIZE / 10). #define BUFFSIZE 100 char buf[100]={0} sprintf(buf, "%%5s %%%ds %%%ds", BUFFSIZE / 10, BUFFSIZE / 10); After running the code above, the buf will contain the string, %10s %10s 回答1: The purpose is to get a format string to use it in another function that needs a

How to unformat xml file

…衆ロ難τιáo~ 提交于 2020-01-03 03:37:09
问题 I have a method which returns a String with a formatted xml. The method reads the xml from a file on the server and parses it into the string: Esentially what the method currently does is: private ServletConfig config; InputStream xmlIn = null ; xmlIn = config.getServletContext().getResourceAsStream(filename + ".xml") ; String xml = IOUtils.toString(xmlIn); IOUtils.closeQuietly(xmlIn); return xml; What I need to do is add a new input argument, and based on that value, continue returning the

Left truncate using python 3.5 str.format?

爷,独闯天下 提交于 2020-01-03 02:41:11
问题 Q: Is is possible to create a format string using Python 3.5's string formatting syntax to left truncate? Basically what I want to do is take a git SHA: "c1e33f6717b9d0125b53688d315aff9cf8dd9977" And using only a format string, get the display only the right 8 chars: "f8dd9977" Things Ive tried: Invalid Syntax >>> "{foo[-8:]}".format(foo="c1e33f6717b9d0125b53688d315aff9cf8dd9977") >>> "{foo[-8]}".format(foo="c1e33f6717b9d0125b53688d315aff9cf8dd9977") >>> "{:8.-8}".format(

Parsing a Powershell variable

筅森魡賤 提交于 2020-01-02 23:02:35
问题 You have all been a great help - let me start by saying that. I get the below output from a function I run - the data is stored in a variable called $response , obtained from an Invoke-RestMethod call. @{ResourceType=UserStory; Id=202847; Name=Professional Lines: 2,800,000 Policy Aggregate Limit update} How do i go about parsing these values into new variables so the end result is TP Id:202847 Professional Lines: 2,800,000 Policy Aggregate Limit update 回答1: What you're showing is the