Tags "string" Result

String vs string

In C# there are String objects and string objects. What is the difference between the two? What are the best practices regarding which to use? Solution There is no difference. string (lower case) is just an alias for System.String.

it1352 1 2019-05-06

String.Format alternative in C

I don't have much experience working with C . Rather I have worked more in C# and so, I wanted to ask my question by relating to what I would have done in there. I have to generate a specific format of the string, which I have to pass to another function. In C#, I would have easily generated the string through the below simple code. string a = "t

it1352 0 2019-05-10

How to get a formatted std::string in c without length liminations

What is the best way (short, using standard libraries and easy to understand) to do this in c : std::string s = magic_command("%4.2f", 123.456f) without length limitations (char s[1000] = ...) where "%4.2f" is any c format string (which would be given to printf for example) I am aware of the snprintf malloc combo suggested for pure c in wri

it1352 0 2019-05-13

Check whether a string is not null and not empty

How can I check whether a string is not null and not empty? public void doStuff(String str) { if (str != null && str != "**here I want to check the 'str' is empty or not**") { /* handle empty string */ } /* ... */ } Solution What about isEmpty() ? if(str != null && !str.isEmpty()) Be sure to use the par

it1352 0 2019-05-21

Checking if 2 strings contain the same characters?

Is there a way to check if two strings contain the same characters. For example, abc, bca -> true aaa, aaa -> true aab, bba -> false abc, def -> false Solution Turn each string into a char[], sort that array, then compare the two. Simple. private boolean sameChars(String firstStr, String secondStr) { char[] first = firstStr.toChar

it1352 1 2019-05-22

String.comparison performance (with trim)

I need to do alot of high-performance case-insensitive string comparisons and realized that my way of doing it .ToLower().Trim() was really stupid due do all the new strings being allocated So I digged around a little and this way seems preferable: String.Compare(txt1,txt2, StringComparison.OrdinalIgnoreCase) The only problem here is that I

it1352 1 2019-05-10

Display Float as String with at Least 1 Decimal Place

I want to display a float as a string while making sure to display at least one decimal place. If there are more decimals I would like those displayed. For example: 1 should be displayed as 1.0 1.2345 should display as 1.2345 Can someone help me with the format string? Solution Use ToString(".0###########") with as much # as decimals you want.

it1352 1 2019-05-10

What is the backslash character (\\)?

What is the string literal \\ backslash? What does it do? I have thought about it but I do not understand it. I also read it on wikipedia. When I try to print the following: System.out.println("Mango \\ Nightangle"); the output is: Mango \ Nightangle What is the significance of this string literal? Solution \ is used as for escape sequence in

it1352 1 2019-05-21

C String Length?

How should I get the number of characters in a string in C ? Solution If you're using a std::string, call length(): std::string str = "hello"; std::cout << str << ":" << str.length(); // Outputs "hello:5" If you're using a c-string, call strlen(). const char *str = "hello"; std::cout << str << ":" << strlen

it1352 1 2019-05-10