在Java中使用trim()删除空间 [英] removing the space using trim() in java

查看:163
本文介绍了在Java中使用trim()删除空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,


我有一个显示添加部门字段的jsp.这是我的问题是如果它为空并单击搜索,然后显示警报消息,请输入部门名称.它不允许重复的值.
我的问题是,如果我将部门名称作为总空间,那么它也将空值放在数据库中.但是我的要求是当我将部门名称作为所有空间时,它还必须显示消息,请输入部门名称.我使用的代码如下.


Hi friends,


I have a jsp which displays the Add Department Field.Here my problem is if it is empty and click on search then it is displaying the alert message as please enter department name.It should not allow the duplicate values.
And my problem is if i give the Department Name as total space then also it is taking and placing that empty value in DB.But my requirement is when i give the department name as all spaces then also it has to display the message as please enter the department name.The code i am using is as below.


if(sub_dept_name == '' || sub_dept_name.trim() == '')
{
     alert("please enter sub department name");
     document.getElementById('txt_sub_deptname').focus();
     return false;
}


我在.js文件中编写的这段代码

谁能帮忙吗?


this code i wrote in .js file

can any one help please.

推荐答案

使用IsEmpty()). com/javase/6/docs/api/java/lang/String.html> String [ length [ ^ ]属性.
Use the appropriate method (IsEmpty()) of the String[^] class.
Use the length[^] property of string.


尝试一下...
try this one...
sub_dept_name = trim(sub_dept_name);
if(sub_dept_name == null || sub_dept_name.length == 0)
{
     alert("please enter sub department name");
     document.getElementById('txt_sub_deptname').focus();
     return false;
}

function trim(s) {
	if(s != null && s.length > 0) {
		while (s.substring(0,1) == ' ') {
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
			s = s.substring(0,s.length-1);
		}
		if(s.search(/[\S]/) == -1) {
			s="";
		}
	}
	return s;
}


谢谢&问候,
尼拉·索尼(Nial Soni)


Thanks & Regards,
Niral Soni


这篇关于在Java中使用trim()删除空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆