Replace uppercase for lowercase

As I am scraping descriptions from sites some appear completely in uppercase. Is there a swift way of replacing "all" uppercase chars for lowercase?

Even better would be if I could have a way to set the first letter and all ". " beginning with uppercase.

Last shot

The script execute without errors but does not change anything in the variable. The text enter and exit as uppercase. This is the complete version:

String description = dataRecord.get("DESCRIPTION");

if (description != null)

{
java.io.StringReader in = new java.io.StringReader(description.toLowerCase());
boolean precededBySpace = true;
StringBuffer properCase = new StringBuffer();
while(true)
{
int i = in.read();
if (i == -1) break;
char c = (char)i;
if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',')
{
properCase.append(c);
precededBySpace = true;
}
else
{
if (precededBySpace)
{
properCase.append(Character.toUpperCase(c));
}
else
{
properCase.append(c);
}
precededBySpace = false;
}
}

return properCase.toString();
}

dataRecord.put("DESCRIPTION", description);

}

else

description = "";

Well, you've still got the

Well, you've still got the "return" line in there, so that halts the script on that line. I think that's your problem.

I would suggest just leaving the function be as I sent it though ... it is more reuseable that way.

Changing to upper or

Changing to upper or lowercase in a script is easy.

String.toLowerCase()

There is no Java Method to use title case, but there ways to do it.

String title(theString)
{
java.io.StringReader in = new java.io.StringReader(theString.toLowerCase());
boolean precededBySpace = true;
StringBuffer properCase = new StringBuffer();
while(true)
{
int i = in.read();
if (i == -1) break;
char c = (char)i;
if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',')
{
properCase.append(c);
precededBySpace = true;
}
else
{
if (precededBySpace)
{
properCase.append(Character.toUpperCase(c));
}
else
{
properCase.append(c);
}
precededBySpace = false;
}
}

return properCase.toString();
}

title(String);

This looks perfect Jason!

This looks perfect Jason!

From what is probably due to my weak java skills I keep running into errors like "Error in method invocation: No args method toLowerCase() not found in class'[Ljava.lang.String;'" when I try to get the script to run.

Could you point me in the right direction? I'd like to apply the trick to variables like DESCRIPTION and FEATURES before submission?

Look at the Java

Look at the Java docs:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

I tried.. I really tried....

I tried.. I really tried....

I get the String.toLowerCase() to work fine if I run it separately but the script refuse to produce any output.

I initiate the script like this:

String fixBig = session.getVariable("DESCRIPTION");

replace the "theString" with fixBig in the script and at the end:

session.setVariable("DESCRIPTION", fixBig);

But no output, nor error, show.

Also, you have "title(String);" at the bottom of the script. Does that fellow really belong there?

Appreciate the help,

So, you scraped a value named

So, you scraped a value named DESCRIPTION, and you want to change it, then set it as a session variable, right?

If assumptions are correct:

// Local reference to variable
fixBig = dataRecord.get("DESCRIPTION");

// Check the value exists
if (fixBig!=null)
fixBig = fixBig.toLowerCase();
else
fixBig = "";

// Set
session.setVariable("DESCRIPTION", fixBig);

Guess I'm not being clear

Guess I'm not being clear here. I got the .toLowerCase() to work fine but when I try to apply your complete script "There is no Java Method to use title case, but there ways to do it." it refuse to execute.

I figured that would contribute to replicate a more standard text format with a Cap letters after sentences ending with "." and and so forth. Did I get that part wrong?

Well, at least I turned all uppercase to lowercase.

Thanks,

Same variable then would

Same variable then would be:

// Function to change the string
String title(theString)
{
java.io.StringReader in = new java.io.StringReader(theString.toLowerCase());
boolean precededBySpace = true;
StringBuffer properCase = new StringBuffer();
while(true)
{
int i = in.read();
if (i == -1) break;
char c = (char)i;
if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',')
{
properCase.append(c);
precededBySpace = true;
}
else
{
if (precededBySpace)
{
properCase.append(Character.toUpperCase(c));
}
else
{
properCase.append(c);
}
precededBySpace = false;
}
}

return properCase.toString();
}

// Local reference to variable
description = dataRecord.get("DESCRIPTION");
// Call the function
description = title(description);

dataRecord.put("DESCRIPTION", description);

This guy refuse to play ball.

This guy refuse to play ball. I used a copy- paste of your version now:

// Function to change the string
String title(description)
{
java.io.StringReader in = new java.io.StringReader(description.toLowerCase());
boolean precededBySpace = true;
StringBuffer properCase = new StringBuffer();
while(true)
{
int i = in.read();
if (i == -1) break;
char c = (char)i;
if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',')
{
properCase.append(c);
precededBySpace = true;
}
else
{
if (precededBySpace)
{
properCase.append(Character.toUpperCase(c));
}
else
{
properCase.append(c);
}
precededBySpace = false;
}
}

return properCase.toString();
}

// Local reference to variable
description = dataRecord.get("DESCRIPTION");
// Call the function
description = title(description);

dataRecord.put("DESCRIPTION", description);

I'm not sure what you're

I'm not sure what you're telling me, Johan. You're still able to scrape the DESCRIPTION, but you run this script and you're not seeing the change? Can you put in a session log between running the function and putting in the dataRecord to just show the current string of description?

I can email you a sample of this script in use, but it's really simple.