使用string.trim()方法应注意的问题

15 篇文章 0 订阅
7 篇文章 0 订阅

当我门判断传过来的参数是否为空时应该这么写if(str == null || str.trim().equals(""))

应注意的是不能把两个倒过来写,会出现空指针错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 dlgConnectSetup 连接设置 2 dlgSaveSymbol 储存代码 3 dlgSymbolSetup 代码设置 4 filequiry 文件查询 5 frmMachineQuery 设备查询界面 6 frmMain 主界面 7 frmMeetArtiQuery 会议论文查询模块 8 frmPeriodArtiQuery 期刊论文查询模块 9 frmProjectQuery 项目查询模块 10 frmRecordInput 录入模块 11 MachineQuery 设备查询 12 moneyquery 经费查询 13 cwbhquery 财务编号查询 14 ProjectQuery 项目查询 15 modMain 初始化模块 16 clsDataSource 类模块 17 drpMachine 设备统计报表 18 drpMeetArticle 会议论文统计 19 drpPeriodArticle 期刊论文统计 20XX drpProArchieve 项目成果报表 21 drpProjFinancial 项目经费 22 drpProjOverview 项目总览 dlgConnectSetup Option Explicit Private Sub CancelButton_Click() Unload Me End Sub Private Sub cmdHelp_Click() frmMain.Hhopen1.OpenHelp App.HelpFile, "Connect.html" End Sub Private Sub Form_Load() Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 txtServerName.Text = GetSetting("科研项目管理系统", "Connection", "ServerName", "") txtDatabaseName.Text = GetSetting("科研项目管理系统", "Connection", "DatabaseName", "") End Sub Private Sub OKButton_Click() If Trim(txtServerName.Text) <> "" And Trim(txtDatabaseName.Text) <> "" Then SaveSetting "科研项目管理系统", "Connection", "ServerName", Trim(txtServerName.Text) SaveSetting "科研项目管理系统", "Connection", "DatabaseName", Trim(txtDatabaseName.Text) Unload Me MsgBox "注意:必须重新启动用程序才能使设置生效。", vbExclamation, "连接设置" Else MsgBox "输入不完全!", vbExclamation, "错误" End If End Sub Private Sub txtServerName_GotFocus() With txtServerName .SelStart = 0 .SelLength = Len(.Text) End With End Sub Private Sub txtdatabaseName_GotFocus() With txtDatabaseName .SelStart = 0 .SelLength = Len(.Text) End With End Sub dlgSaveSymbol Option Explicit Private Sub CancelButton_Click() Unload Me End Sub Private Sub OKButton_Click() Dim RegKey As String If Trim(txtString.Text) <> "" Then Select Case dlgSaveSymbol.Caption Case "添加项目性质代号" RegKey = "Symbol\ProjectQuality" SaveSetting "科研项目管理系统", RegKey, txtNumber.Text, txtString.Text SaveSetting "科研项目管理系统", RegKey, "Count", txtNumber.Text Case "编辑项目性质代号" RegKey = "Symbol\ProjectQuality" SaveSetting "科研项目管理系统", RegKey, txtNumber.Tex
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace dlapss { public partial class Frm_Login : Form { public Frm_Login() { InitializeComponent(); } //登录 private void btt_Login_Click(object sender, EventArgs e) { loginValidate(); } /// <summary> /// 登录验证方法 /// </summary> public void loginValidate() { if (txt_UserloginId.Text.Trim() == "" || string.IsNullOrEmpty(txt_UserloginId.Text)) { MessageBox.Show("用户名不能为空!", "登录提示"); txt_UserloginId.Focus(); } else if (txt_UserPass.Text.Trim() == "" || string.IsNullOrEmpty(txt_UserPass.Text)) { MessageBox.Show("密码不能为空!", "登录提示"); txt_UserPass.Focus(); } else if (cbo_loginType.Text.Trim() == "" || string.IsNullOrEmpty(cbo_loginType.Text.Trim())) { MessageBox.Show("请选择登录类型!", "登录提示"); } else { UserInfo u = null; SqlConnection con = new SqlConnection(DBHelper.conStr); try { con.Open(); int userRole = cbo_loginType.SelectedIndex; string sql = string.Format("select * from userInfo where UserloginId='{0}' and UserPass='{1}' and UserRole={2}", txt_UserloginId.Text, txt_UserPass.Text, userRole); SqlCommand com = new SqlCommand(sql, con); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { u = new UserInfo(); u.UserId = Convert.ToInt32(dr["UserId"]); u.UserloginId = dr["UserloginId"].ToString(); u.UserName = dr["UserName"].ToString(); u.UserPass = dr["UserPass"].ToString(); u.UserRole = dr["UserRole"].ToString(); } dr.Close(); if (u != null) { LoginInfo.LoginUserInfo = u;//保存登录用户信息 this.Visible = false; Frm_Main fm = new Frm_Main(); fm.Show(); } else { MessageBox.Show("用户名或密码错误!", "登录提示"); } } catch (Exception) { MessageBox.Show("请注意数据库连接字符串!", "登录提示"); } finally { con.Close(); } } } //重置 private void btt_Reset_Click(object sender, EventArgs e) { txt_UserloginId.Text = ""; txt_UserPass.Text = ""; txt_UserloginId.Focus(); } private void cbo_loginType_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter)//当点击回车键 { if (txt_UserloginId.Text != "" && txt_UserPass.Text != "") loginValidate(); else SendKeys.Send("{TAB}"); } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值