Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Monday, July 16, 2018

How to convert from a LIST to a string in Python?

Methods of conversion are needed in every language.

You can convert a list to string as shown here for a simple list:



ListToString_1

Here is an example of conversion of a more complicated list from an earlier post:


ListToString_2

Wednesday, November 29, 2017

Does PowerShell work with XML?

XML is stored as a string in PowerShell. Hence it can work with XML.

For example I have a XML document such as this one:

http://hodentek.blogspot.com/2007/07/simple-xml-file-aka-my-hello-world-for.html
-----------------


Review image 

-------------
How do I use this XML document?

Just assign this string to a PowerShell string variable as shown:(note the XML formatting has been changed slightly)
-------------

Monday, November 7, 2016

Is there a JSON validator in SQL Server?

Sure there is if you are using SQL Server 2016.

The Transact-SQL IsJSON() tests whether a expression(string) is JSON valid. If it is valid you should get a 1 as return value, a zero(0) if it is not valid and a null if the expression is null.

How do you use it?

This is a json string, a very simple one:
=================
{"wclass":{"student":["jay", "john", "sam"]}}
====================
The following code snippet shows how you may use it:
===========
declare @json nvarchar(150)SET @json=N'{"wclass":{"student":["jay", "john", "sam"]}}';
Select ISJSON(@json)

==========
When you run this in the SQL Server 2016 query pane, you get the return value 1 (see image below).


Wednesday, June 10, 2015

How do you convert a string to integer in Powershell?


It is easy to convert a number represented as a string into an Integer.

Here is a string declaration:
$str1="123"

Declare the string in the command window is as shown:
PS C:\Users\Jayaram> $str1="123"  

The displayed value in the command window is as shown:
PS C:\Users\Jayaram> $str1
123

You can get to find the data type by running the following:
PS C:\Users\Jayaram> $str1.GetType().FullName
System.String

==============================
Now we convert this to an integer

The following line converts the string to an Integer
PS C:\Users\Jayaram> $foo=[INT]$str1

This is the value in the $foo variable
PS C:\Users\Jayaram> $foo
123

Now we get the type of data in $foo
PS C:\Users\Jayaram> $foo.GetType().FullName
System.Int32

Difference between GetType().FullName and just GetType()
 

PowerShell has intellisense support and you should make use of it in your coding as shown here:
After typing $foo insert a . and wait for the pop-up menu with various possible items as shown.

                                                                                                           
A string such as "555-5555-1212" is a string but will return an error as the variable is not in the correct format.
PS C:\Users\Jayaram> $str2="555-5555-1212"

PS C:\Users\Jayaram> $boo=[INT]$str2
Cannot convert value "555-5555-1212" to type "System.Int32". Error: "Input
string was not in a correct format."
At line:1 char:1
+ $boo=[INT]$str2
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger