How to calculate the length of the string using C#?

Ankith Reddy
Updated on 20-Jun-2020 11:42:42

10K+ Views

Use the String.Length property in C# to get the length of the string.str.LengthThe property calculates the words in the string and displays the length of the specified string, for example, the string Amit has 4 characters −string str = "Amit";ExampleThe following is the C# program to calculate the string length − Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "Amit";          Console.WriteLine("String: "+str);          Console.WriteLine("String Length: "+str.Length);          Console.ReadKey();       }    } }OutputString: Amit String Length: 4

How can we change MySQL user password by using the SET PASSWORD statement?

Govinda Sai
Updated on 20-Jun-2020 11:42:26

244 Views

We can use SET PASSWORD statement to change the password. Before using this command, we need to have at least UPDATE privileges. Its syntax would be as follows −SyntaxSET PASSWORD FOR ‘user_name@host_name’=new_password;Here, New_password would be new password we want to set for MySQL userUser_name is the name of the current user.Host_name is the name of the host of the current user.ExampleSuppose if we want to change the password user@localhost to ‘tutorials’ then it can be done as follows −SET PASSWORD FOR ‘user@localhost’= tutorials;

How can we change MySQL user password by using the ALTER USER statement?

mkotla
Updated on 20-Jun-2020 11:41:28

266 Views

We can also use ALTER USER statement along with IDENTIFIED BY clause to change MySQL user password. Its syntax would be as possible −SyntaxALTER USER user_name@host_name IDENTIFIED BY ‘new_password’Here, New_password would be new password we want to set for MySQL userUser_name is the name of a current user.Host_name is the name of the host of a current user.ExampleSuppose if we want to change the password user@localhost to ‘tutorials’ then it can be done as follows −ALTER USER user@localhost IDENTIFIED BY ‘tutorials’

How to capture divide by zero exception in C#?

Samual Sam
Updated on 20-Jun-2020 11:40:58

3K+ Views

System.DivideByZeroException is a class that handles errors generated from dividing a dividend with zero.ExampleLet us see an example −using System; namespace ErrorHandlingApplication {    class DivNumbers {       int result;       DivNumbers() {          result = 0;       }       public void division(int num1, int num2) {          try {             result = num1 / num2;          } catch (DivideByZeroException e) {             Console.WriteLine("Exception caught: {0}", e);          } finally {             Console.WriteLine("Result: {0}", result);          }       }       static void Main(string[] args) {          DivNumbers d = new DivNumbers();          d.division(25, 0);          Console.ReadKey();       }    } }OutputThe values entered here is num1/ num2 −result = num1 / num2;Above, if num2 is set to 0, then the DivideByZeroException is caught since we have handled exception above.

How can I use SUBSTRING_INDEX() function to get the substring as output which is between two same delimiters in a string?

Samual Sam
Updated on 20-Jun-2020 11:40:33

174 Views

We need to use nested SUBSTRING_INDEX() function for getting the substring as output which is between two same delimiters in a string. For example, from the string ‘www.tutorialspoint.com’, we want the substring ‘tutorialspoint’, which is in between two same delimiters ‘.’ as output then SUBSTRING_INDEX() function can be used in nested form as follows −mysql> Select SUBSTRING_INDEX(SUBSTRING_INDEX('www.tutorialspoint.com','.',2),'.',-1)AS 'Nested SUBSTRING_INDEX'; +------------------------+ | Nested SUBSTRING_INDEX | +------------------------+ | tutorialspoint         | +------------------------+ 1 row in set (0.02 sec)

How to concatenate two strings in C#?

George John
Updated on 20-Jun-2020 11:40:10

678 Views

To concatenate two strings, use the String.Concat method.Let’s say you want to concatenate two strings in C#, str1 and str2, then add it as arguments in the Concat method −string str3 = string.Concat(str1, str2);ExampleThe following is the example − Live Demousing System; class Program {    static void Main() {       string str1 = "Brad";       string str2 = "Pitt";       // Concat strings       string str3 = string.Concat(str1, str2);       Console.WriteLine(str3);    } }OutputBradPitt

How to use an assignment operator in C#?

karthikeya Boyini
Updated on 20-Jun-2020 11:39:47

90 Views

Assign value to a variable using the assignment operator in C# −The following are the assignment operators in C# −OperatorDescriptionExample=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B assigns value of A + B into C+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C – A*=Multiply ... Read More

How to define custom methods in C#?

Samual Sam
Updated on 20-Jun-2020 11:38:03

817 Views

To define a custom method in C#, use the following syntax − (Parameter List) { Method Body }The following are the various elements of a method −Access Specifier − This determines the visibility of a variable or a method from another class.Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared ... Read More

How to define dynamic data types in C#

Arjun Thakur
Updated on 20-Jun-2020 11:36:55

170 Views

You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time. C# 4.0 introduced the dynamic type that avoids compile time type checking.The following is the syntax for declaring a dynamic type −dynamic = value;Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time.Let us see an example −dynamic a = 25;To get the type of dynamic variable −Exampleusing System; namespace Demo { ... Read More

Why we do not have global variables in C#?

Samual Sam
Updated on 20-Jun-2020 11:35:21

147 Views

C# does not have global variables and the scope resolution operator used in C++ for global variables is related to namespaces. It is called global namespace alias.If you have a type that shares an identifier in a different namespace, then to identify them using the scope resolution operator.For example, to reference System.Console class, use the global namespace alias with the scope resolution operator −global::System.ConsoleLet us now see an example −Example Live Demousing myAlias = System.Collections; namespace Program {    class Demo {       static void Main() {          myAlias::Hashtable h = new myAlias::Hashtable();     ... Read More

Advertisements