I m very confused with the string replacing methods of objective c.
Please tell where to use ReplaceCharacterInRange method and Where to use
stringByReplacingOccurrenceOfString Method.
These two methods differ a lot.
replaceStringWithCharactersInRange: withString:replaces all characters in the given range with the new string. It works on a NSMutableString and changes the string object you call it on.
In contrast stringByReplacingOccurrencesOfString:withString: replaces all occurrences of a given string, but returns a new string object. So it does work with immutable string as well.
So you use the first method if you want to keep your string but change parts of it while you use the second when you want to replace certain substrings within the string without changing the original string.
Related
I am debugging an application that runs on a server and users will access the application on another server. The application uses encryption and as part of the key, I am using the String.Reverse property.
Dim Mystring As String = "123abc"
Dim reverse = String.Format("{0},{1}", Mystring.Reverse)
The string reverse is different when I run it from one machine (RDP/Citrix Environment ASP.NET 4.6.1). The value is:
System.Linq.Enumerable+<ReverseIterator>d__a2`1[System.Char]
The same string, but ran from another machine (RPD non-Citrix Environment ASP.NET 4.5.2). The value of reverse is:
System.Linq.Enumerable+<ReverseIterator>d__73`1[System.Char]
Why are the values different in the different environments?
Look at this line first:
Dim reverse = String.Format("{0},{1}", Mystring.Reverse)
Specifically, this expression:
Mystring.Reverse
Reverse is a function, not a property, but it's missing the parentheses (). The trick here is the String.Format() method accepts the base Object type as an argument, and compiler is able to treat the MyString.Reverse expression as a delegate type that is convertible to object. The values you see in your output are the result of calling .ToString() on that function delegate. It's the type name for the function, rather than anything to do with the value of your MyString object. Since that type is dynamically and randomly generated at runtime, you'll see different values not only on different platforms, but different runs on the same computer.
In the VB6 era, it was normal to call methods without the parentheses. In the .Net world, always use parentheses when you call a method.
What you want is this:
Dim reverse As String = String.Format("{0},{1}", Mystring.Reverse())
Even here, you're missing the second argument to match the format string. I doubt you'll get the result you expect.
Finally, reversing a string as the key seems very wrong when it comes to encryption. You are using a real cyrptogrpahic algorithm from the System.Security.Cryptography library, right? Right!?
You are not outputting the value of the reversed String but the name of the type used to perform the reversal. That type is dynamically created and randomly named. The "d" in those two names means "dynamic" and the "a2" and "73" parts are random.
Basically, what you perceive to be an issue is not an issue. The problem is that you're not actually creating a String from the reversed output. You say "String.Reverse property but that is NOT a property. It is a method and it is not a member of the String class but rather an extension method on the IEnumerable(Of T) interface. You are treating your String as an enumerable list of Char values and reversing that. If you want a String from that then you need to create one, i.e.
MyReversedString = New String(Mystring.Reverse().ToArray())
That will push the contents of your iterator into an array and then create a new String object from that array.
When I save a string to redis using ServiceStack.Redis:
client.Add("key1","abc");
While fetching the value, it returns:
client.GetValue("key1");
it returns
"\"abc\""
How do I get the complete string?
Thanks
It appears as though the client.Add() method converts the value to a string (even strings) and wraps them in quotes. The client.SetValue() method only accepts strings and does not wrap them in quotes.
One option would be to convert the value into a string yourself. Either via the common ToString() method, or another method to get the needed string from the object.
If the Add() method is necessary however. You could opt to check if the string is wrapped in quotes when you get it via GetValue() and if so, remove them.
Redis converts string to JSON when saving, that's why it's wrapped in quotes.
So you have to treat this string as JSON object and parse it afterwards manually or using deserialization.
Smalltalk - Is it possible to add a string to a String instance via a method?
Essentially I'd like something along the lines of:
renderThisOn: aString
aString append: 'whatever text I want'
Essentially I'd like a String instance (ByteString, etc) to behave like the "html" object in Seaside. I pass it on as an argument to multiple methods, each adding some information to it.
From a practical viewpoint the answer would be no, it is not possible to change the size of a String. You can modify the characters of the String though:
a := 'abc'.
a at: 2 put: $x.
a = 'axc' "true"
Therefore, when you concatenate two strings you get a third one, while the others two remain unchanged
s := 'abc'.
t := 'def'.
u := s , t.
s = 'abc'. "true"
t = 'def'. "true"
Having said this, there is actually a way to do grow (or shrink) a String. The idea is to use become: (or becomeForward:). This message will replace all references to the receiver with references to the argument. In your case:
s := 'abc'.
t := 'def'.
assoc := s -> 3 "referene to s"
s become: s , t.
s = 'abcdef'. "true"
assoc key == s "true"
The reason why I started my answer by saying that you cannot change the string's size is because in the vast majority of cases the use of become: is overkilling and the recommended practice is to review the code and eliminate the need for modifying the structure of an object.
String literals in Smalltalk are immutable objects.
It is possible different ways:
Pass instance of WriteStream
renderThisOn: aWriteStream
aWriteStream nextPutAll: 'whatever text I want'
You can create own class wrapper around String and pass its instance:
renderThisOn: aStringWrapper
aStringWrapper append: 'whatever text I want'
Streams are more preferable, because streams are faster than string concatenation
comma method:
|a|
a := 'abc'.
a,'def'
ctrl+p and get 'abcdef'
Depending on how you want to use it, a string holder can achieve what you need. The simplest might be to pass an array with the first and only element being the string you want to alter in different methods. The array won't change but the one and only element will change as you place different strings there. More complex would be to create a specialised string holder class, with a field that holds the string which changes. You can then implement the common string methods on that holder, to redirect to the current actual string.
I currently write some code to decode string using NSStringEncoding.
And I'd like to decode that string using all value of NSStringEncoding.
But I don't know how to get all value of NSStringEncoding.
I checked this article, but values of NSStringEncoding it not continuous,
so I'm looking for better solution.
looping through enum values
Anyone have good idea??
You can use NSString's class method availableStringEncodings which:
Returns a zero-terminated list of the encodings string objects support in the application’s environment.
Described another way a "zero-terminated list" is a pointer to a C-array. You can iterate over this array.
HTH
we can use both the method while passing parameters to sql query ?
then wht is the difference between them?
The MSDN article on AddWithValue() explains the difference between the 2. Within is the explaination on the subtle differences.
AddWithValue replaces the SqlParameterCollection.Add method that takes a String and an Object.
The overload of Add that takes a string and an object was deprecated because of possible ambiguity with the SqlParameterCollection.Add overload that takes a String and a SqlDbType enumeration value where passing an integer with the string could be interpreted as being either the parameter value or the corresponding SqlDbType value.
Use AddWithValue whenever you want to add a parameter by specifying its name and value.