String intern

In this chapter you will learn:

  1. How to intern a string

Intern a string

using System;// j  a v a 2 s .  c o  m
using System.Text;

class Main
{
  static void Main(string[] args)
  {
    string sLiteral = "Automatically Interned java2s.com";
        string sNotInterned = "Not " + sLiteral;

        TestInterned( sLiteral );
        TestInterned( sNotInterned );

        String.Intern( sNotInterned );
        TestInterned( sNotInterned );

        string string1 = "";
        String string2 = "";

        string1 = "asdf";
        string2 = "asdf1";
        
  }
       
    static void TestInterned( string str )
    {
        if( String.IsInterned( str ) != null )
        {
            Console.WriteLine( "The string \"{0}\" is interned.", str );
        }
        else
        {
            Console.WriteLine( "The string \"{0}\" is not interned.", str );
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. Normalize unicode string
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String