[Solved] Replacing a substring with given form with another in C#

Basically: take the pattern (containing 3 groups), replace instr with second group from pattern. private string MyReplace(string inStr, string leaveStr) { string pattern = @”(.*?)(” + leaveStr + “)(.*)”; string repl = @”*$2*”; Regex rgx = new Regex(pattern); return rgx.Replace(inStr, repl); } string x = MyReplace(@”\textbf\{atext\}”, “atext”); x = MyReplace(@”\textbf\{1\}”, “1”); full string – group … Read more

[Solved] How to process a string? [closed]

Declare a variable to count the stars with an appropriate data type. Iterate (loop) over the string to check each character for equality with ‘*’. If this is the case then increment your star counter. Width and height are not required. If you want to constrain a larger field to the width and height provided, … Read more

[Solved] Split a string C#

You can use Regex.Matches(): string source = “Mobile: +49 (123) 45678Telephone: +49 (234) 567890Fax: +49 (345) 34234234”; string[] phones = Regex .Matches(source, “[A-Za-z]+: \\+([0-9)( ])+”) .Cast<Match>() .Select(i => i.ToString()) .ToArray(); OR You can use IndexOf(): string source = “Mobile: +49 (123) 45678Telephone: +49 (234) 567890Fax: +49 (345) 34234234”; var telephoneIndex = source.IndexOf(“Telephone”, StringComparison.InvariantCulture); var faxIndex … Read more

[Solved] C++ why std::string is much slower than C string

Quite interesting results. I’ve re-run the benchmark on 4.8.2 20140120 and got: strcmp : 1.938 secs std::string.compare(const char* s) : 1.842 secs std::string == std::string : 1.225 secs strncmp : 2.660 secs memcmp : 1.182 secs std::string.compareN : 1.711 secs strstr : 5.854 secs memmem : 1.187 secs std::string.find : 14.363 secs So std::string behaves … Read more

[Solved] In java its not calculating correctly

I suggest you not use double at all here as it is clearly confusing you. If you are going to use double, you should always round your results instead of rounding down. Additionally as 0.1 and 0.01 and 0.05 cannot be represented accurately, you should avoid using them. int iPenny = (int) Math.round((iDollarTotal – iTen … Read more

[Solved] Java string end with space

Use String.trim() to remove surrounding whitespace and then use String.equals() (not ==, See 15.21 Equality Operators in the Java Language Specification for full details.). Remember that String instances are immutable so String.trim() returns a new String and it is that which must be used in the equals() check. Note that trim() removes leading whitespace also. … Read more

[Solved] Java Sentence Analysis Check

Your code is confuse, but for what I understood, you want to analyse a certain phrase. First, the constructor of the class that handles the analyse must receive the String to analyse, since it is it’s job. (Every class has a job, the result it’s obtained from the classes methods). Second, there is a convention … Read more

[Solved] Why does entering “Δ to a Scanner result in the wrong character?

The value 65533 is 0xFFFD. This is the Unicode “Replacement Character” which is used in place of a character that is unrepresentable. It is normally displayed as “�”. The reason you are getting this could be because your standard input (keyboard?) is not capable of producing the character “Δ. Try putting this character into a … Read more

[Solved] How to invert a word? [closed]

You can use StringBuffer or StringBuilder for this task, StringBuilder would be my choice since its more efficient. its not thread safe so multiple threads can call its methods simultaneously. String reversedString = new StringBuilder(originalString).reverse().toString() If you prefer not to use API support you can do something like this static String reverse(String stringIn) { char[] … Read more

[Solved] When do you use a String over int [closed]

Conventionally, you use the type that best fits your scenario. If you need to do math associated with a number, use a datatype capable of that, like int, double, long, float, etc. If you don’t need to perform math on it, or you’re using it as a label, then a String is acceptable. (In all … Read more