string mutation - string

I am trying to create a string mutation that, after a prompt for a city and state, would output the state in uppercase, followed directly by the city in lowercase, followed directly by the state again in uppercase.
I have tried many types of mutations but nothing is working.
Can anyone help me?

use String#toUpperCase() and String#toLowerCase() methods.
eg. System.out.println(state.toUpperCase());

Here is one in java
Scanner sc =new Scanner(System.in);
String city,state;
System.out.println("Enter City =");
city=sc.nextLine();
System.out.println("Enter State =");
state=sc.nextLine();
System.out.println( state.toUpperCase() + " "+city.toLowerCase() + " "+ state.toUpperCase());
Independent of any programming language(But dependent on character Representation) you could retrieve every character of string and add 22 which would convert UPPERCASE into LOWERCASE.As you must be knowing ASCII values of a-z is 97-122 and that of A-Z is 65-90.so you can lookout how much to add/ subtract to convert between cases.

Related

How do I take a string and turn it into a list using SCALA?

I am brand new to Scala and having a tough time figuring this out.
I have a string like this:
a = "The dog crossed the street"
I want to create a list that looks like below:
a = List("The","dog","crossed","the","street")
I tried doing this using .split(" ") and then returning that, but it seems to do nothing and returns the same string. Could anyone help me out here?
It's safer to split() on one-or-more whitespace characters, just in case there are any tabs or adjacent spaces in the mix.
split() returns an Array so if you want a List you'll need to convert it.
"The dog\tcrossed\nthe street".split("\\s+").toList
//res0: List[String] = List(The, dog, crossed, the, street)

Regular expression for splitting a comma with the string

I had to split string data based on Comma.
This is the excel data:-
Please find the excel data
string strCurrentLine="\"Himalayan Salt Body Scrub with Lychee Essential Oil from Majestic Pure, All Natural Scrub to Exfoliate & Moisturize Skin, 12 oz\",SKU_27,\"Tombow Dual Brush Pen Art Markers, Portrait, 6-Pack\",SKU_27,My Shopify Store 1,Valid,NonInventory".
Regex CSVParser = new Regex(",(?=(?:[^\"]\"[^\"]\")(?![^\"]\"))");
string[] lstColumnValues = CSVParser.Split(strCurrentLine);
I have attached the image.The problem is I used the Regex to split the string with comma but i need the ouptut just like SKU_27 because string[0] and string2 contains the forward and backward slash.I need the output string1 and remove the forward and backward slash.
The file seems to be a CVA file. For CVA to be properly formatted, it will use quotes "" to wrap strings that contains comma, such as
id, name, date
1,"Some text, that includes comma", 2020/01/01
Simply split the string by comma, you will get the 2nd column with double quote.
I'm not sure whether you are asking how to remove the double-quotes from lstColumnValues[0] and lstColumnValues[2], or add them to lstColumnValues[1].
To remove the double-quotes, just use Replace:
string myString = lstColumnValues[0].Replace("\"", "");
If you need to add them:
string myString = $"\"{lstColumnValues[1]}\"";

SPLIT results with separator

I'm trying to split a string (separated with the HTML break tag), without deleting the break tag. I think it's pretty messy to add a break as string after splitting, so is there any function/possibility to keep the separator while "splitting"?
Example:
<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>
Expected result:
<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>
As far as I know SPLIT removes the separator from the results and it doesn't seem like you can change that.
But you could create your own separator by first replacing your <br/> tag with <br/> plus an arbitrary string that is highly unlikely to ever appear in your HTML source, and then split the HTML using this arbitrary string as a separator instead.
types:
begin of t_result,
segment(2000) type c,
end of t_result.
DATA:
source type string,
separator type string,
brtag type string,
repl type string,
result_tab type standard table of t_result,
result_row TYPE t_result.
brtag = '<br/>'.
separator = '|***SEP***|'.
concatenate brtag separator into repl.
source = '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.
replace all occurrences of brtag in source with repl.
split source at separator into table result_tab.
LOOP AT result_tab INTO result_row.
WRITE:
result_row-segment.
ENDLOOP.
Output of that example report:
<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>
The caveat of this solution is that your custom separator, if not chosen with some care, might appear in your HTML source on its own. I therefore would choose an arbitrary string with a special character or two that would be encoded in HTML (like umlauts) and therefore not appear in your source.
Just use the replace command. replace <br/> with <br/>CR_LF
The CR_LF refers to the carriage return linefeed character.
In more complex cases you can use regex expressions in abap.
class ZTEST_SO definition public create public .
public section.
methods t1.
ENDCLASS.
CLASS ZTEST_SO IMPLEMENTATION.
METHOD T1.
data: my_break type string,
my_string type string
value '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.
my_break = '<br/>' && CL_ABAP_CHAR_UTILITIES=>CR_LF.
replace all occurrences of '<br/>' in my_string with my_break in character mode.
"check my_string in the debugger :)
"<HTML><BODY><p>some text<br/>
"some more text</p></BODY></HTML>
ENDMETHOD.
ENDCLASS.

Reading Words for a String

I want to manipulate this string:
"Roger:Rabbit:22:California"
and display the output as follows:
Name: Roger Rabbit
Age: 22
State: California
I am wondering what will be the best approached to this?
For this you may use the String split() method. Mention your delimiter as :. Then you want to make sure you create an array out of the output of your split method with the different strings split. In your System.out.println() you can mention the array index of your newly created array elements as System.out.println("Name" + myIndex[0]).
Resource: https://www.tutorialspoint.com/java/java_string_split.htm

Add new character in between a string builder string

I am building a new string using string builder. But now if want to add new characters in between already existing characters in the stringbuilder. How do i do it?
Example code:
StringBuilder sbr = new StringBuilder(" ");
sbr.append(1);
sbr.append(" ");
sbr.append(2);
sbr.append(" ");
sbr.append("3");
sbr.append(" ");
Now the string looks like 1 2 3
I want to add a new string after the number two. Can anyone please guide me how to do that?
Use the following to insert the character at position 3
sbr.insert(2, "<new charactor>");

Resources