How to remove part of a string?
My favourite way of doing this is “splitting and popping”: split() splits a string into an array of strings using a specified separator string.pop() removes the last element from an array and returns that element.
My favourite way of doing this is “splitting and popping”: split() splits a string into an array of strings using a specified separator string.pop() removes the last element from an array and returns that element.
The most naive way would be to iterate over the String and make sure all the elements are valid digits for the given radix. This is about as efficient as it could possibly get, since you must look at each element at least once. I suppose we could micro-optimize it based on the radix, but … Read more
Use str.replace. Alternatively use re and use regular expressions. This will allow the removal of leading/trailing spaces.
In Java I can pass a Scanner a string and then I can do handy things like, scanner.hasNext() or scanner.nextInt(), scanner.nextDouble() etc. This allows some pretty clean code for parsing a string that contains rows of numbers. How is this done in C# land? If you had a string that say had: In Java I … Read more
String are immutable in Java. You can’t change them. You need to create a new string with the character replaced. Or you can use a StringBuilder:
If by string you mean std::string you can do it with this method: QString QString::fromStdString(const std::string & str) If by string you mean Ascii encoded const char * then you can use this method: QString QString::fromAscii(const char * str, int size = -1) If you have const char * encoded with system encoding that can be read with QTextCodec::codecForLocale() then you should use … Read more
You don’t need to copy a Python string. They are immutable, and the copy module always returns the original in such cases, as do str(), the whole string slice, and concatenating with an empty string. Moreover, your ‘hello’ string is interned (certain strings are). Python deliberately tries to keep just the one copy, as that … Read more
Why do you think that your method is not efficient? It’s actually one of the most efficient ways that you can do it. You should of course read the character into a local variable or use an enumerator to reduce the number of array accesses: One thing that makes a method like this efficient is … Read more
To read a whole line, use rather than You might consider renaming nameFileout since it isn’t a name, and is for input not output.
I would like to know how to convert a string containing digits to a double.