Strings adalah kumpulan karakter yang dianggap sebagai satu buah
unit tersendiri. Karakter ini dapat berupa huruf besar, huruf kecil, angka
maupun karakter spesial seperti +, &, $, # dll. String adalah object dari
class String di namespace System. Kita menuliskan string biasanya
diletakkan dalam tanda petik.
"Ini adalah string!"
Untuk inisialisasi dan deklarasi kita tinggal menggunakan,
string strColor = "blue";
Sama seperti array, string dapat mengetahui jumlah karakter pada
dirinya dengan menggunakan property Length. Karena sebenarnya
string adalah array dari karakter. Jadi kita dapat menggunakan format
array untuk melihat indeks pada string. Misalnya kita ingin tahu huruf
pertama, maka tinggal gunakan strColor[0].
Pada praktikum kali ini kita akan mencoba memanipulasi string dengan
berbagai method yang telah disediakan di .NET.
Praktikum
praktikum disini saya akan mencoba membuat program pemisahan, penggabungan, pemasukan, pengganti, dsb menggunakan string.
langkah langkah nya sebagai berikut
1. buka mic visual studio atau sejenisnya
2. lalu desain program nya, toolbox yang digunakan di program ini : textbox, label, button, groupbox,
3. susun bagian bagian toolbox nya seperti gambar
4. setelah itu copy code berikut dan masukan kedalam programnya
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace praktikum_minggu6
{
public partial class Form1 : Form
{
string str1 = "My string ";
string str2 = "My string is a long string ";
public Form1()
{
InitializeComponent();
textBox1.Text = str1;
textBox2.Text = str2;
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt16(textBox4.Text);
string y = Convert.ToString(textBox5.Text);
string test1 = str1.Insert(a, y) ;
textBox17.Text = test1;
}
private void button2_Click(object sender, EventArgs e)
{
int a = Convert.ToInt16(textBox6.Text);
int y = Convert.ToInt16(textBox7.Text);
string test2 = str1.Remove(a, y);
textBox17.Text = test2;
}
private void button3_Click(object sender, EventArgs e)
{
string test3 = str1.ToLower();
textBox17.Text = test3;
}
private void button4_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox18.Text);
string[] z = str1.Split();
textBox17.Text = string.Join(a, z);
}
private void button5_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox11.Text);
string y = Convert.ToString(textBox10.Text);
string z = Convert.ToString(textBox12.Text);
string[] x = { a, y };
string test8 = String.Join(z, x);
textBox17.Text = test8;
}
private void button6_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox9.Text);
int y = Convert.ToInt16(textBox8.Text);
int test5 = str2.IndexOf(a, y);
textBox3.Text = str2.Substring(test5);
}
private void button7_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox14.Text);
int y = Convert.ToInt16(textBox13.Text);
int test6 = str2.LastIndexOf(a, y);
textBox3.Text = str2.Substring(test6);
}
private void button8_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox15.Text);
string y = Convert.ToString(textBox16.Text);
string test7 = str2.Replace(a, y);
textBox3.Text = test7;
}
private void button9_Click(object sender, EventArgs e)
{
string test10 = str1.Trim();
textBox17.Text = test10;
}
private void button10_Click(object sender, EventArgs e)
{
string test4 = str1.ToUpper();
textBox17.Text = test4;
}
private void button11_Click(object sender, EventArgs e)
{
textBox17.Text = "" ; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = "";
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace praktikum_minggu6
{
public partial class Form1 : Form
{
string str1 = "My string ";
string str2 = "My string is a long string ";
public Form1()
{
InitializeComponent();
textBox1.Text = str1;
textBox2.Text = str2;
}
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt16(textBox4.Text);
string y = Convert.ToString(textBox5.Text);
string test1 = str1.Insert(a, y) ;
textBox17.Text = test1;
}
private void button2_Click(object sender, EventArgs e)
{
int a = Convert.ToInt16(textBox6.Text);
int y = Convert.ToInt16(textBox7.Text);
string test2 = str1.Remove(a, y);
textBox17.Text = test2;
}
private void button3_Click(object sender, EventArgs e)
{
string test3 = str1.ToLower();
textBox17.Text = test3;
}
private void button4_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox18.Text);
string[] z = str1.Split();
textBox17.Text = string.Join(a, z);
}
private void button5_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox11.Text);
string y = Convert.ToString(textBox10.Text);
string z = Convert.ToString(textBox12.Text);
string[] x = { a, y };
string test8 = String.Join(z, x);
textBox17.Text = test8;
}
private void button6_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox9.Text);
int y = Convert.ToInt16(textBox8.Text);
int test5 = str2.IndexOf(a, y);
textBox3.Text = str2.Substring(test5);
}
private void button7_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox14.Text);
int y = Convert.ToInt16(textBox13.Text);
int test6 = str2.LastIndexOf(a, y);
textBox3.Text = str2.Substring(test6);
}
private void button8_Click(object sender, EventArgs e)
{
string a = Convert.ToString(textBox15.Text);
string y = Convert.ToString(textBox16.Text);
string test7 = str2.Replace(a, y);
textBox3.Text = test7;
}
private void button9_Click(object sender, EventArgs e)
{
string test10 = str1.Trim();
textBox17.Text = test10;
}
private void button10_Click(object sender, EventArgs e)
{
string test4 = str1.ToUpper();
textBox17.Text = test4;
}
private void button11_Click(object sender, EventArgs e)
{
textBox17.Text = "" ; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = "";
}
}
}
5. lalu coba run programnya gan, kalo contoh di gambar ini menggunakan join, yaitu menggabungkan beberapa string.
Tugas:
di tugas ini saya akan mencoba untuk membuat program untuk memisah-misahkan satu paket protokol.
langkah langkah nya sebagai berikut:
1. buka mic.visual studio
2. buat form yang berisikan toolbox: label, textbox,groupbox, & button
3.desain program nya
4. lalu masukan kodenya ke dalam program
ini kodingan nya:
public partial class ParseData : Form
{
private string str="*&(ikh)(abcde304900015halo apa kabar?okeh5986edcbaio test*0iou";
private int dat=15;
private int sour=2;
private int trail=4;
private int cr=4;
private int h=5;
public ParseData()
{
InitializeComponent();
textBox1.Text=str;
}
void parsing()
{
string dt=str.Substring(22,dat);
string s=str.Substring(13,sour);
string d=str.Substring(15,sour);
string t=str.Substring(37,trail);
string c=str.Substring(41,cr);
string hl=str.Substring(17,h);
data.Text=dt;
source.Text=s;
destination.Text=d;
trailer.Text=t;
crc.Text=c;
hlen.Text=hl;
}
private void btn_parse_Click(object sender, EventArgs e)
{
parsing();
}
private void closebtn_Click(object sender, EventArgs e)
{
Close();
}
}
5. lalu run programnya!
baiklah sekian untuk minggu ini apabila ada kesalan mohon maaf, Semoga bermanfaat :)








No comments:
Post a Comment