|
GString是Gtk+中非常实用的一个字符串操作结构,可以使对C语言中字符串操作指针不熟悉的程序员,快速的操作字符串,而且不出错,下面的代码演示了GString操作Append(追加),Insert(指定位置插入),OverWrite(指定位置复写),Erase(指定位置删除)和Equal(比较)
作者:AlimysoYang- #include <gtk/gtk.h>
- GtkWidget *wnd;
- GtkWidget *vbox,*hbox,*et1,*et2,*et3,*btn,*btninsert,*btnoverwrite,*btnerase,*btnequal;
- void ui_init()
- {
- wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_title(GTK_WINDOW(wnd),"GString about prepend");
- gtk_window_set_position(GTK_WINDOW(wnd),GTK_WIN_POS_CENTER);
-
- vbox = gtk_vbox_new(TRUE,2);
- gtk_container_add(GTK_CONTAINER(wnd),vbox);
-
- et1 = gtk_entry_new();
- et2 = gtk_entry_new();
- et3 = gtk_entry_new();
- gtk_entry_set_editable(GTK_ENTRY(et3),FALSE);
-
- hbox = gtk_hbox_new(TRUE,2);
- btn = gtk_button_new_with_label("GString prepend");
- btninsert = gtk_button_new_with_label("GString insert");
- btnoverwrite = gtk_button_new_with_label("GString overwrite");
- btnerase = gtk_button_new_with_label("GString erase");
- btnequal = gtk_button_new_with_label("GString equal");
-
- gtk_box_pack_start_defaults(GTK_BOX(vbox),et1);
- gtk_box_pack_start_defaults(GTK_BOX(vbox),et2);
- gtk_box_pack_start_defaults(GTK_BOX(vbox),et3);
- gtk_box_pack_start_defaults(GTK_BOX(vbox),hbox);
-
- gtk_box_pack_start_defaults(GTK_BOX(hbox),btn);
- gtk_box_pack_start_defaults(GTK_BOX(hbox),btninsert);
- gtk_box_pack_start_defaults(GTK_BOX(hbox),btnoverwrite);
- gtk_box_pack_start_defaults(GTK_BOX(hbox),btnerase);
- gtk_box_pack_start_defaults(GTK_BOX(hbox),btnequal);
-
- gtk_widget_show_all(wnd);
- }
- static void btn_clicked(GtkButton *button,gpointer user_data)
- {
- const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
- const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
- GString *gstr = g_string_new(str1);
- g_string_prepend(gstr,str2);
- g_string_up(gstr);
- gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
- g_string_free(gstr,TRUE);
- }
- static void btninsert_clicked(GtkButton *button,gpointer user_data)
- {
- const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
- const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
- GString *gstr = g_string_new(str1);
- int len = gstr->len;
- int pos = len - 2;
- if (pos<0)
- pos = 0;
- g_string_insert(gstr,pos,str2);
- g_string_down(gstr);
- gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
- g_string_free(gstr,TRUE);
- }
- static void btnoverwrite_clicked(GtkButton *button,gpointer user_data)
- {
- const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
- const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
- GString *gstr = g_string_new(str1);
- int len = gstr->len;
- int pos = len - 2;
- if (pos<0)
- pos = 0;
- g_string_overwrite(gstr,pos,str2);
- gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
- g_string_free(gstr,TRUE);
- }
- static void btnerase_clicked(GtkButton *button,gpointer user_data)
- {
- const gchar *str = gtk_entry_get_text(GTK_ENTRY(et1));
- GString *gstr = g_string_new(str);
- int len = gstr->len;
- gssize pos = len - 4;
- g_string_erase(gstr,pos,2);
- gtk_entry_set_text(GTK_ENTRY(et3),gstr->str);
- g_string_free(gstr,TRUE);
- }
- static void btnequal_clicked(GtkButton *button,gpointer user_data)
- {
- const gchar *str1 = gtk_entry_get_text(GTK_ENTRY(et1));
- const gchar *str2 = gtk_entry_get_text(GTK_ENTRY(et2));
- GString *gstr1,*gstr2;
- gstr1 = g_string_new(str1);
- gstr2 = g_string_new(str2);
- if (g_string_equal(gstr1,gstr2))
- g_printf("the same strings\n");
- else
- g_printf("the different strings\n");
- g_string_free(gstr1,TRUE);
- g_string_free(gstr2,TRUE);
- }
- int main(int argc,char *argv[])
- {
- gtk_init(&argc,&argv);
-
- ui_init();
-
- g_signal_connect(G_OBJECT(wnd),"destroy",G_CALLBACK(gtk_main_quit),NULL);
- g_signal_connect(G_OBJECT(btn),"clicked",G_CALLBACK(btn_clicked),NULL);
- g_signal_connect(G_OBJECT(btninsert),"clicked",G_CALLBACK(btninsert_clicked),NULL);
- g_signal_connect(G_OBJECT(btnoverwrite),"clicked",G_CALLBACK(btnoverwrite_clicked),NULL);
- g_signal_connect(G_OBJECT(btnerase),"clicked",G_CALLBACK(btnerase_clicked),NULL);
- g_signal_connect(G_OBJECT(btnequal),"clicked",G_CALLBACK(btnequal_clicked),NULL);
- gtk_main();
- return 0;
- }
复制代码 |
评分
-
查看全部评分
|