segunda-feira, 12 de agosto de 2019

C# - convertendo string num array de string

Aqui está um exemplo de como converter uma string
num array de string.

Veja abaixo uma imagem do programa em execução:



Veja abaixo o código do programa:


//C# - CONVERTENDO STRING NUM ARRAY DE STRING
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Arr {

    public partial class Form1 : Form {
        System.Text.StringBuilder str_1 = new System.Text.StringBuilder ( );
        string st = " ";
        /*============================================================*/
        void Informe ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Helvetica", 11,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( System.Drawing.Color.Red );
            Brush myBrush_2 = new SolidBrush ( System.Drawing.Color.Blue );
            Brush myBrush_3 = new SolidBrush ( System.Drawing.Color.Black );
            dc.DrawString ( "Por: ", myFont, myBrush_1, 170, 210 );
            dc.DrawString ( "Samuel Lima ", myFont, myBrush_2, 200, 210 );
            dc.DrawString ( "sa_sp10@hotmail.com ", myFont, myBrush_3, 170, 225 );
        }
        /*============================================================*/
        public void Ini_Array ( ) {
            int i = 0;
            do {
                if ( i % 10 == 0 ) {
                    st += "\n";
                }
                if ( i >= 0 && i <= 9 )
                    st += ( "0" );
                st += i + " ";
                i++;
            } while ( i < 100 );
            string [ ] strBd = new string [ 10 ];
            i = 0;
            strBd [ i ] = st;
            str_1.Append ( strBd [ i ] );
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            this.Size = new System.Drawing.Size ( 600, 300 );
            this.Text = "C# - CONVERTENDO STRING NUM ARRAY DE STRING";
            this.BackColor = Color.LightGray;
            Graphics dc = e.Graphics;
            Pen BluePen = new Pen ( Color.Blue, 10 );
            dc.DrawRectangle ( BluePen, 5, 5, 575, 250 );
            Font myFont_1 = new System.Drawing.Font ( "Consolas", 12,
                FontStyle.Italic );
            Font myFont_2 = new System.Drawing.Font ( "Consolas", 11,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            Brush myBrush_2 = new SolidBrush ( Color.Black );
            dc.DrawString ( "C# - CONVERTENDO STRING NUM ARRAY DE STRING",
                myFont_1, myBrush_1, 100, 15 );
            dc.DrawString ( " " + str_1, myFont_2, myBrush_2, 170, 20 );
            Informe ( e );
        }
        /*============================================================*/
        public Form1 ( ) {
            Ini_Array ( );
        }
    }
    /*============================================================*/
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}

C# - convertendo string em array de char

Aqui está um exemplo de como converter uma string num array de caracteres.

Veja abaixo uma imagem do programa em execução:




Veja abaixo o código do programa:




//C# - CONVERTENDO STRING NUM ARRAY DE CHAR

//APAGANDO VÁRIOS CARACTERES
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Arr {

    public partial class Form1 : Form {
        System.Text.StringBuilder str = new System.Text.StringBuilder ( );
        System.Text.StringBuilder str_1 = new System.Text.StringBuilder ( );
        /*============================================================*/
        void Informe ( PaintEventArgs e ) {
            Graphics dc = e.Graphics;
            Font myFont = new System.Drawing.Font ( "Helvetica", 11,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( System.Drawing.Color.Red );
            Brush myBrush_2 = new SolidBrush ( System.Drawing.Color.Blue );
            Brush myBrush_3 = new SolidBrush ( System.Drawing.Color.Black );
            dc.DrawString ( "Por: ", myFont, myBrush_1, 150, 210 );
            dc.DrawString ( "Samuel Lima ", myFont, myBrush_2, 180, 210 );
            dc.DrawString ( "sa_sp10@hotmail.com ", myFont, myBrush_3, 150, 225 );
        }
        /*============================================================*/
        public void Ini_Array ( ) {
            int i;
            char [ ] str = new char [ 100 ];
            string st
            = "A definição de saúde possui implicações legais,\n"
              + "sociais e econômicas dos estados de saúde e doença;\n"
              + "sem dúvida, a definição mais difundida é a encontrada\n"
              + "no preâmbulo da Constituição da Organização Mundial\n"
              + "da Saúde: saúde é um estado de completo bem-estar físico,\n"
              + "mental e social,e não apenas a ausência de doenças.\n";
            //Converte a string num array de char
            str = st.ToCharArray ( );
            int n = 56;
            int n_1 = 72;
            for ( i = n; i < n_1; i++ ) {
                str [ i ] = '\0';
                str [ i ] = ' ';
            }
            for ( i = 0; i < str.Length; i++ ) {
                str_1.Append ( str [ i ] );
            }
        }
        /*============================================================*/
        protected override void OnPaint ( PaintEventArgs e ) {
            this.Size = new System.Drawing.Size ( 600, 300 );
            this.Text = "C# - CONVERTENDO STRING NUM ARRAY DE CHAR";
            this.BackColor = Color.LightGray;
            Graphics dc = e.Graphics;
            Pen BluePen = new Pen ( Color.Blue, 10 );
            dc.DrawRectangle ( BluePen, 5, 5, 575, 250 );
            Font myFont_1 = new System.Drawing.Font ( "Consolas", 14,
                FontStyle.Italic );
            Font myFont_2 = new System.Drawing.Font ( "Consolas", 12,
                FontStyle.Italic );
            Brush myBrush_1 = new SolidBrush ( Color.Red );
            Brush myBrush_2 = new SolidBrush ( Color.Black );
            dc.DrawString ( "C# - CONVERTENDO STRING NUM ARRAY DE CHAR",
                myFont_1, myBrush_1, 80, 15 );
            dc.DrawString ( "" + str_1, myFont_2, myBrush_2, 30, 40 );
            Informe ( e );
        }
        /*============================================================*/
        public Form1 ( ) {
            Ini_Array ( );
        }
    }
    /*============================================================*/
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main ( ) {
            Application.EnableVisualStyles ( );
            Application.SetCompatibleTextRenderingDefault ( false );
            Application.Run ( new Form1 ( ) );
        }
    }
}

Abaixo outro exemplo usando o console do windows.


//C# - convertendo string em array de char
using System;
using System.Collections;

class Arr {
    //System.Text.StringBuilder str = new System.Text.StringBuilder ( );
    System.Text.StringBuilder str_1 = new System.Text.StringBuilder ( );
    /*============================================================*/
    void Barra ( int lin, int col ) {
        int i, j;
        for ( i = 0; i < lin; i++ )
            for ( j = 0; j < col; j++ ) {
                Console.SetCursorPosition ( i, j );
                Console.BackgroundColor = ConsoleColor.DarkBlue;
                Console.Write ( " " );
            }
    }
    /*============================================================*/
    void Informe ( ) {
        Console.BackgroundColor = ConsoleColor.White;
        Console.SetCursorPosition ( 20, 16 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "Por: " );
        Console.SetCursorPosition ( 25, 16 );
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write ( "Samuel Lima" );
        Console.SetCursorPosition ( 20, 17 );
        Console.ForegroundColor = ConsoleColor.Black;
        Console.Write ( "sa_sp10@hotmail.com" );
        Console.SetCursorPosition ( 30, 19 );
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "MUITO OBRIGADO" );
    }
    /*============================================================*/
    void Moldura ( int tam_lin_ini, int tam_lin_fim, int tam_ini_col,
        int tam_fim_col ) {
        int i, c;
        for ( i = tam_lin_ini; i < tam_lin_fim; i++ ) {
            for ( c = tam_ini_col; c < tam_fim_col; c++ ) {
                Console.SetCursorPosition ( c, i );
                Console.BackgroundColor = ConsoleColor.White;
                Console.Write ( " " );
            }
        }
    }
    /*============================================================*/
    void Ini_Array ( ) {
        char [ ] str = new char [ 100 ];
        string st
        = "A definição de saúde possui implicações legais,\n"
          + "sociais e econômicas dos estados de saúde e doença;\n"
          + "sem dúvida, a definição mais difundida é a encontrada\n"
          + "no preâmbulo da Constituição da Organização Mundial\n"
          + "da Saúde: saúde é um estado de completo bem-estar físico,\n"
          + "mental e social,e não apenas a ausência de doenças.\n";
        int i;
        //Converte a string num array de char
        str = st.ToCharArray ( );
        int n = 56;
        int n_1 = 72;
        for ( i = n; i < n_1; i++ ) {
            str [ i ] = '\0';
            str [ i ] = ' ';
        }
        //Posiciona os textos no console
        Console.SetCursorPosition ( 10, 4 );
        Console.ForegroundColor = ConsoleColor.Blue;
        for ( i = 0; i < str.Length; i++ ) {
            str_1.Append ( str [ i ] );
        }
        Console.Write ( str_1 );
        Console.ReadKey ( );
    }
    /*============================================================*/
    public static void Main ( ) {
        Console.Title = ( "C# - CONVERTENDO STRING NUM ARRAY DE CHAR" );
        Arr array = new Arr ( );
        array.Moldura ( 1, 20, 2, 68 );
        Console.SetCursorPosition ( 15, 2 );
        Console.BackgroundColor = ConsoleColor.White;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write ( "C# - CONVERTENDO STRING NUM ARRAY DE CHAR" );
        //array.Informe ( );
        array.Ini_Array ( );
        //Segura a execução do programa
        Console.ReadKey ( );
    }
}
/*============================================================*/