ToUpper and ToLower part of a string in .net C++?
-
Thanks for looking at my question, I am wondering how I would use the ToUpper and ToLower functions on only part of a string in .net C++. For example, I converted an inputted name to all caps for the purpose of comparing it. Now I need to display the name with only the first letter capitalized. In short, I need to make "BRAD" to "Brad" Note that the inputted name is not always the same, so however you can show me how to do this has to work with any inputted name. Thanks for any help!!
-
Thanks for looking at my question, I am wondering how I would use the ToUpper and ToLower functions on only part of a string in .net C++. For example, I converted an inputted name to all caps for the purpose of comparing it. Now I need to display the name with only the first letter capitalized. In short, I need to make "BRAD" to "Brad" Note that the inputted name is not always the same, so however you can show me how to do this has to work with any inputted name. Thanks for any help!!
TabascoSauce wrote:
with only the first letter capitalized.
Sentence case? You can write code using SubString(), ToUpper() and ToLower(). Here it is.
String^ ConvertToSentenceCase(String^ input)
{
return (input->Length > 0) ? String::Concat(input->Substring(0,1)->ToUpper(),input->Substring(1)->ToLower())
: String::Empty;
}Other methid is to use
TextInfo
class and work withToTitleCase
method. Here it isString^ ConvertToSentenceCase(String^ input)
{
using namespace System::Threading;
return Thread::CurrentThread->CurrentCulture->TextInfo->ToTitleCase(input);
}Hope that helps :)
Navaneeth How to use google | Ask smart questions
-
TabascoSauce wrote:
with only the first letter capitalized.
Sentence case? You can write code using SubString(), ToUpper() and ToLower(). Here it is.
String^ ConvertToSentenceCase(String^ input)
{
return (input->Length > 0) ? String::Concat(input->Substring(0,1)->ToUpper(),input->Substring(1)->ToLower())
: String::Empty;
}Other methid is to use
TextInfo
class and work withToTitleCase
method. Here it isString^ ConvertToSentenceCase(String^ input)
{
using namespace System::Threading;
return Thread::CurrentThread->CurrentCulture->TextInfo->ToTitleCase(input);
}Hope that helps :)
Navaneeth How to use google | Ask smart questions
Hey thanks for the help! Sadly I am still having issues, after I tried to build it the build log gives me the error: "1>.\TempConverter.cpp(37) : error C2440: 'return' : cannot convert from 'System::String ^' to 'int' 1> No user-defined-conversion operator available, or 1> There is no context in which this conversion is possible Here's the block of code I have your second suggestion in (yes I am very, very new to programming, I'm trying to overachieve on an assignment in my online class):
//Determining student access and calculating desired conversion if (name->CompareTo("ALPHA" || "BETA" || "GAMMA") != 0) { //Display no-access message & end program Console::WriteLine("Sorry, you have insufficient rights to access to this program"); Console::Write("Please enter 1 to end the program: "); endProgram = Convert::ToInt16(Console::ReadLine()); } else { //Greeting and user input if (name->CompareTo("ALPHA" || "BETA" || "GAMMA") == 0) { **using namespace System::Threading; return Thread::CurrentThread->CurrentCulture->TextInfo->ToTitleCase(name); Console::WriteLine("Hello ", initial->ToUpper() , ". ", name);** Console::Write("Are you converting from celsius or fahrenheit?: "); tempr = Convert::ToString(Console::ReadLine()->ToLower()); }
I'm trying to get it to write something like "Hello M. Beta" Feel free to point anything else I've done wrong (to an expert this probably looks horrible). Again thanks for your time and anymore help you can give me!
modified on Sunday, February 15, 2009 6:31 PM
-
Hey thanks for the help! Sadly I am still having issues, after I tried to build it the build log gives me the error: "1>.\TempConverter.cpp(37) : error C2440: 'return' : cannot convert from 'System::String ^' to 'int' 1> No user-defined-conversion operator available, or 1> There is no context in which this conversion is possible Here's the block of code I have your second suggestion in (yes I am very, very new to programming, I'm trying to overachieve on an assignment in my online class):
//Determining student access and calculating desired conversion if (name->CompareTo("ALPHA" || "BETA" || "GAMMA") != 0) { //Display no-access message & end program Console::WriteLine("Sorry, you have insufficient rights to access to this program"); Console::Write("Please enter 1 to end the program: "); endProgram = Convert::ToInt16(Console::ReadLine()); } else { //Greeting and user input if (name->CompareTo("ALPHA" || "BETA" || "GAMMA") == 0) { **using namespace System::Threading; return Thread::CurrentThread->CurrentCulture->TextInfo->ToTitleCase(name); Console::WriteLine("Hello ", initial->ToUpper() , ". ", name);** Console::Write("Are you converting from celsius or fahrenheit?: "); tempr = Convert::ToString(Console::ReadLine()->ToLower()); }
I'm trying to get it to write something like "Hello M. Beta" Feel free to point anything else I've done wrong (to an expert this probably looks horrible). Again thanks for your time and anymore help you can give me!
modified on Sunday, February 15, 2009 6:31 PM
Aah, you copied my code to a wrong place. Try this
String^ ConvertToSentenceCase(String^ input)
{
using namespace System::Threading;
return Thread::CurrentThread->CurrentCulture->TextInfo->ToTitleCase(input);
}if (name->CompareTo("ALPHA" || "BETA" || "GAMMA") == 0)
{
using namespace System::Threading;
name = ConvertToSentenceCase(name);
Console::WriteLine("Hello ", initial->ToUpper() , ". ", name);
// rest of the code
}:)
Navaneeth How to use google | Ask smart questions
-
Aah, you copied my code to a wrong place. Try this
String^ ConvertToSentenceCase(String^ input)
{
using namespace System::Threading;
return Thread::CurrentThread->CurrentCulture->TextInfo->ToTitleCase(input);
}if (name->CompareTo("ALPHA" || "BETA" || "GAMMA") == 0)
{
using namespace System::Threading;
name = ConvertToSentenceCase(name);
Console::WriteLine("Hello ", initial->ToUpper() , ". ", name);
// rest of the code
}:)
Navaneeth How to use google | Ask smart questions
Ahhh, Ok I understand now. Thanks for the help! After I busted through like 100 other errors I had in my program I finally got it to work. You're a lifesaver, have a good day!