Вопрос по содержимому строки....

Dima_XXX
Дата: 14.05.2006 06:43:43
Привет народ!
Столкнулся вот с такой проблемкой, мне нужно проверить содержимое введенной строки, т.е. в ней должны содержаться только буквы или цифры, ни какие другие символы не должны присутствовать....
Мне казалось, что в С# должна быть какая нить функция для проверки, но когда начал искать информацию, то нашел только функции для char...но не для string....
Конечно есть вариант разбирать каждый символ (в цикле), но это как то не то...
Может кто - нибудь знает об этом или кто-то сталкивался...?
Буду рад любому ответу...Спасибо...
Olx
Дата: 14.05.2006 10:29:58
из help VS:

Create the IsNumeric Function
In Visual C# .NET, you can use the Double.TryParse method to obtain functionality that is similar to IsNumeric. Double.TryParse converts the string representation of a number in a specified style and culture-specific format to its double-precision floating point number equivalent. To create the IsNumeric function:
Start Visual Studio .NET. On the File, point to New, and then click Project.
In the New Project dialog box, click Visual C# Projects under Project Type.
Under Templates, click Console Application, and then click OK. By default, Class1.cs is created.
At the end of the Class1 class, add the following code for the IsNumeric function:
// IsNumeric Function
static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
	bool isNum;

// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
	double retNum;
			
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
	isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
	return isNum;
}		
Olx
Дата: 14.05.2006 10:35:45
или так:
using Microsoft.VisualBasic;
...

if (Information.IsNumeric(myString))
{
...
}
Такова жизнь
Дата: 14.05.2006 11:22:36
А регулярные выражения отменили уже?
Sv219
Дата: 14.05.2006 12:38:07
Такова жизнь
А регулярные выражения отменили уже?

поддержу, в данном случае лучше использовать регулярные выражения
Dima_XXX
Дата: 14.05.2006 19:01:52
Olx
или так:
using Microsoft.VisualBasic;
...

if (Information.IsNumeric(myString))
{
...
}


Да, помниться работал с такой функцией но в VB6...
Попробую ее использовать...


Спасибо народ за предложения, попробую поюзать варианты...
И еще один вопрос, а вот как проверить эту самую строку на содержимое и букв и цифр одновременно(Например: Маша115). Ведь если по одельности проверять, то результаты будут ЛОЖЬ..
А кто нибудь знает, как можно еще проверить на содержание символов (%;№...и т.д.).
Dima_XXX
Дата: 14.05.2006 19:05:16
Sv219
Такова жизнь
А регулярные выражения отменили уже?

поддержу, в данном случае лучше использовать регулярные выражения


С удовольствием бы использовал регулярные выражения, но вот не всегда их найти...Хотя, если подумать, то зачем нужны тогда программисты, если на свете будут существовать все! функции, примеры и т.д. которые нужны....
Dima_XXX
Дата: 14.05.2006 19:21:57
Olx
из help VS:

Create the IsNumeric Function
In Visual C# .NET, you can use the Double.TryParse method to obtain functionality that is similar to IsNumeric. Double.TryParse converts the string representation of a number in a specified style and culture-specific format to its double-precision floating point number equivalent. To create the IsNumeric function:
Start Visual Studio .NET. On the File, point to New, and then click Project.
In the New Project dialog box, click Visual C# Projects under Project Type.
Under Templates, click Console Application, and then click OK. By default, Class1.cs is created.
At the end of the Class1 class, add the following code for the IsNumeric function:
// IsNumeric Function
static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
	bool isNum;

// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
	double retNum;
			
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
	isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
	return isNum;
}		


Спасибо за вариант...