Showing posts with label C# Strings. Show all posts
Showing posts with label C# Strings. Show all posts

String splitting C#

split string c#

C#.Net provides Split() function to split the string by the specified char and stores it in array. Split string using C# is bit easy task. In C# we can split string into substrings—such as splitting a sentence into individual words. In this case we set split char as a "," character and then it will returns an array of substrings

using System;
public class sortlist
{
  public static void Main()
  {
    string myString = "C#,VB,ASP.NET,C++";
    string[] splitedString = myString.Split(‘,’); 
    foreach(string item in splitedString)
    {
     Console.WriteLine(item);
    }
  }
}

//Output
//C#
//VB
//ASP.NET
//C++

Base64 to String [C#]

C# convert string to base64

In this post we will see how to convert Base64 string to string. .Net provides .FromBase64String() method in System.Convert class to convert Base64 string to string. While converting Base64 string to string it first converts base64 string to byte array and then byte array converted to string.

Sample code to convert Base64 string to string [C#]


//Convert Base64 string to string
public static string Base64ToString(string strBase64)       
{  
byte[] byteArray = Convert.FromBase64String(strBase64);            
return(System.Text.Encoding.UTF8.GetString(byteArray));        
}

String to Base64 [C#]

string to base64

In this post we will look how to convert string to Base64 using C#. .Net provides .ToBase64String() method in System.Convert class that we can use to convert string to base64. It basically converts value of an array of 8-bit unsigned integers to its equivalent String representation consisting of base 64 digits.

Converting string to Base64 is bit tricky. We need to follow some steps mentioned as below

  • 1. We need to convert string to byte array.
  • 2. then we need to use Convert.ToBase64String() to convert byte array to base64 string.

Sample code to convert string to Base64 string [C#]


str ="How to convert string to nase64 in C#"; 
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str);
// Now convert the byte array to a Base64 string
strBase64 = Convert.ToBase64String(byteArray);

C# Uppercase First Character or Letter

uppercase first letter in string

Some times we need to uppercase the first letter. There are various ways to make first word in uppercase. we will look at some of the ways to uppercase the first letter in strings in the C#. We can take out first character of string from it’s zero index and the using char.ToUpper(str[0]) we can capitalize first character and then concatenate it with remaining string and make string again. Other way to uppercase first character or letter is use .ToCharArray() method and then from array of characters take zero index character make it uppercase and then formulate new string again. Other alternative is to use Substring() method and take characters from position 0 to 1 and capitalize first character and then using substring from 1 position concatenate those strings.

Here we will see how first way will work to uppercase first character or letter of the string using c#.

C# code to uppercases first character

using System;
class Program 
{ 
 static void Main()
 {
  Console.WriteLine(UppercaseFirstLetter("first")); 
 }
 static string UppercaseFirstLetter(string str)
 {
  var  firstChar = str[0];
  var  UpperCaseFirstCharacter =  char.ToUper(firstChar);
  var convertedFirstCharToUpper = UpperCaseFirstCharacter +  str.Substring(1);
  return convertedFirstCharToUpper ;
 }
}


//Result:
//First.

In above code sample for upper case first letter we first take first char as str[0] and using char.ToUpper(str[0]) we Capitalized first character and then concatenated remaining string with first capitalized char and the returns the new string with first letter capitalized.


Second way to Uppercase the First latter of the String:


C# code to uppercases first letter


using System;

class Program
{
 static void Main()
 {
  Console.WriteLine(UppercaseFirstCharacter("first"));    
 }

 static string UppercaseFirstCharacter(string str)
 {
  char[] strArray = str.ToCharArray();
  strArray[0] = char.ToUpper(strArray [0]);
  return new string(strArray);
 }
}


//Result
//First.

In above C# code we have converted input string in to array of characters using .ToCharArray() and then took zero index character and uppercase it and the formed new string with new string constructor to construct string.

Third way to uppercase first character of string using c#

C# code to uppercases first character [C#]


public static string Capitalize(string input)   
{
 str = "first";
 return str.Substring(0, 1).ToUpper() + str.Substring(1);
}


//Result:
//First

In above C# example to uppercase first character of string we used Substring() method. using Substring(0,1) we get first character of string and then we capitalized that character like Substring(0,1).ToUpper() and then we concatenated remaining string from first position onwards to it.

C# string ToLower - Convert string to lowercase

c# string tolower

While writing C# code some times we need a string to be converted to lowercase string, so that the uppercase letters of the string get converted to lowercase letters. This is useful when you need to compare the string with lowercase string. It is good practice to make comparing strings to be convert to uppercase or lowercase if we don’t know how input is coming. We will see how to convert all letters to small case in the string using ToLower function in C#.

C# code that shows how to use ToLower [C#]


using System;
class Program
{
 static void Main()
 {
  string str = "C# STRING LOWERCASE EXAMPLE";
  string upStr = str.ToLower();
  Console.WriteLine(upStr);
 }
}

Result
c# string lowercase example


The ToLower function or method converts all the uppercase characters in the string to lowercase characters, If string contains some lowercase letters or characters it ignores them. We will see how to compare two strings by using lowercase to the input string.

using System;

class Program
{
 static void Main()
 {
  string str = "C# STRING LOWERCASE";
  string upStr = str.ToLower();
  if("c# string lowercase” == upStr)
  Console.WriteLine("Strings are equal.");
 }
}


Result
Strings are equal.

C# string ToUpper - Convert string to uppercase

c# string toupper

In some cases we need to convert lowercase string to uppercase string, so that the lowercase letters of the string get converted  to uppercase letters. We can uppercase string using c# string ToUpper Method This is useful when you need to compare the string with uppercase string. It is good practice to make comparing strings to be convert to uppercase or lowercase if we don’t know how input is coming. We will see how to capitalize all letters in the string using ToUpper function in C#.


C# code that shows how to use ToUpper [C#]


using System;

class Program
{
static void Main()
{
string str = "C# string uppercase example";
string upStr = str.ToUpper();
Console.WriteLine(upStr);
}
}

//Result
//C# STRING UPPERCASE EXAMPLE


The ToUpper function or method converts all the lowercase letters in the string to uppercase letters, If string contains some uppercase letters or characters it ignores them. We will see how to compare two strings by using uppercase the the input string.


using System;

class Program
{
static void Main()
{
string str = "C# string uppercase";
string upStr = str.ToUpper();
if("C# STRING UPPERCASE” == upStr)
Console.WriteLine("Strings are equal.");
}
}

//Result
//Strings are equal.

Some time you may need to just upper case the first latter or character of the string. In that case we can use ToCharArray method and the for first character user char.ToUpper().

C# string

C3 strings
C# string is a object of class String. Value of string is text, which is nothing
but the collections of read-only char Objects. In C# we can do various string operations like
strings splitting, string indexOf, string reverse, string format , we can also use string .ToUpper()
to make string in upper case in C#, also we can use string .ToLower() to make string in
lower case in C#. String also has Trimmimg functions. we can trim the string in
c# using these functions. We will discuss most of these function to perform operations
on string using c#.

C# string is alias for System.String so String and string are equivalent
to each other. The Length property of string shows number of char objects
the string contains. C# string provides lot’s of methods to create and to manipulate,
to compare, to split…etc.


C# string is immutable, means we can not change contents of string once object
is get created. When ever we change the contents of string new object of string
get created. We will see examples of c# string operations and manipulations. Below
samples will show how to make string in upper case in c# and how to make string
in lower case.

1. ToUpper() method is used to convert string in upper case.

Sample code to shows how to convert string to upper case[c#]


string myString = "This is C# string" 
string toUpperString = myString.ToUpper(); 

2. ToLower() method is used to convert string in lower case. Below example
shows how to convert string in lower case:


string myString = "This is C# string"
string tolowerString = myString.ToLower();


3. Trim() method is used to remove all white spaces from both start and end
of the string. 

string myString = " This is C# string "
string trimedString = myString.Trim();

TrimStart() method is used to trim string at start of string and TrimEnd()
is used to trim the string at the end.

TrimStart() C# example:


string myString = " This is C# string ";
string trimedString = myString.TrimStart();

TrimEnd() C# example:


string myString = " This is C# string " ;
string trimedString = myString.TrimEnd(); 

We can strip out specified characters from start of the string or at the end of
the string. Example below shows how to strip out characters from start and end position
TrimSart() C# example:


string myString = " This is C# string "; string 
trimedString = myString.TrimStart(); 
4. Replace() function used to replace string/char from the given string.

string myString = "This is VB string";
string newString = myString.Replace(“VB”, “C#”);


//Above line of code will replace “VB” by “C#”. 

5. Split() function used to split the string by the specified char and stores
it in array. Below code will split string by “,” and will store result in array
of string. Splitting a string into substrings—such as splitting a sentence into
individual words—is a common programming task. The Split() method takes a char array
of delimiters, for example, a space character, and returns an array of substrings.


string myString = "C#,VB,ASP.NET,C++";
string[] splitedString = myString.Split(‘,’); 

6. ToString() method provided by string class converts a value to a string.
Below C# code shows how .ToString() will convert numeric values into string:


int year = 1999;
string msg = year.ToString();

7. Substring() Function finds the specified characters form the string starts from 0 of the string to the used position. Substring functions takes integer parameter, which specifies from what position we need to substring the string. Below Substring sample code shows how to get substring from 8th position till end:


string myString = " This is C# string "
string newSubString = myString.Substring(8);

So result of substring of above code will take string “C# string”. We can also specify
the range from which position to which position we need to get substring.

Example below how Substring() works in c#:


string myString = " This is C# string ";
string newSubString = myString.Substring(8, 2);
Result of above code will be string “C#”. 

8.IndexOf() function is used to find String within string and returns index
position of the string. If specified character or string has not found in string
then it returns -1 index.


string myString = " This is C# string ";
int newSubString = myString.IndexOf (“C#”);
Above code will return 8th index position.

9. ToCharArray() Copies the characters in this instance to a Unicode character
array. We can use then this array to reverse the string.Below is the C# code to
Reverse the string


string myString = "string";
char[] stringArray = myString.ToCharArray();
Array.Reverse(stringArray);
string
newString = new string(stringArray);


Result of above code is “gnirts”.

Concatenate strings:In C# we can "add" two strings of text together
to create one string. The process of adding two strings together is called string
concatenation.


string s1 = "Learn C# ";
string s2 = "Strings";
string newString = s1 + s2;
So newString will hold the concatenated string “Learn C# Strings”.

The @ SymbolThe @ symbol tells the string constructor to ignore
escape characters and line breaks. The following two strings are therefore identical:

string p1 = "\\\\My Documents\\My Files\\";
string p2 = @\\My Documents\My Files\