Java - Replacing word with assigned numbers - replace

I would like to make this code shorter and cleaner.
This code works properly, but It is to long for me:
String givenWord = "help";
String partA = givenWord.replace("a", "1");
String partB = partA.replace("b", "2");
String partC = partB.replace("c", "10");
String partD = partC.replace("d", "11");
String partE = partD.replace("e", "12");
String partF = partE.replace("f", "20");
String partG = partF.replace("g", "21");
String partH = partG.replace("h", "22");
String partI = partH.replace("i", "100");
String partJ = partI.replace("j", "101");
String partK = partJ.replace("k", "102");
String partL = partK.replace("l", "110");
String partM = partL.replace("m", "111");
String partN = partM.replace("n", "112");
String partO = partN.replace("o", "120");
String partQ = partO.replace("q", "121");
String partP = partQ.replace("p", "122");
String partR = partP.replace("r", "200");
String partS = partR.replace("s", "201");
String partT = partS.replace("t", "202");
String partU = partT.replace("u", "210");
String partV = partU.replace("v", "211");
String partW = partV.replace("w", "212");
String partX = partW.replace("x", "220");
String partY = partX.replace("y", "221");
String partZ = partY.replace("z", "222");
System.out.println(partZ);
I need to replace all letters in the given word like that:
The output is 2220110122.
I have tried to use 'for loops' and other methods, but they didn't work well.
I am a beginner, that's why I am asking here :)

Related

CSV File to Salesforce records

I have a custom object Employee with fields below. The employee data is maintained in an external system which is sending a base64 encoded string of the csv extract by calling a web service.
I am able to decode the string by using EncodingUtil.base64Decode(). My question is how can I prepare inserts from the decoded base64 string in Salesforce custom object.
String File = 'U25vLE5hbWUsTGFzdCBOYW1lLEVtcCBJZCxKb2IgRnVuY3Rpb24NCjEsU2FjaGluLENob3VyYXNpeWEsMzMwLEJ1c2luZXNzDQoyLFJhamF0ICxTYXhlbmEgLDMzNCxGdW5jdGlvbmFsDQo=';
String myFile = EncodingUtil.base64Decode(file).toString();
System.debug('Sachin'+'['+ myFile + ']');
Debug logs
I found an answer by myself. Please take a look and provide feedback.
String File = 'U25vLE5hbWUsTGFzdCBOYW1lLEVtcCBJZCxKb2IgRnVuY3Rpb24NCjEsU2FjaGluLENob3VyYXNpeWEsMzMwLEJ1c2luZXNzDQoyLFJhamF0ICxTYXhlbmEgLDMzNCxGdW5jdGlvbmFsDQo=';
String myFile = EncodingUtil.base64Decode(file).toString();
System.debug('Sachin'+'['+ myFile + ']');
List<String> EmployeeList = new List<String>();
EmployeeList = myFile.split('\n');
List<Employee__c> employeeInsertList = new List<Employee__c>();
for (String employee : EmployeeList)
{
List<String> fields = new List<String>();
fields = employee.split(',');
Employee__c empRecord = new Employee__c();
empRecord.LastName__c = fields[2];
empRecord.Name = fields[1];
empRecord.EmpId__c = fields[3];
empRecord.Job_Function__c = fields[4];
employeeInsertList.add(empRecord);
}
System.debug('Employee List is '+employeeInsertList);
insert employeeInsertList;

How to use TokenSequencePattern

I'm just getting started with CoreNLP's TokenSequencePattern and I can't get simple matches to work. All im trying to do is to match a token from the input text. The code below executes without errors but doesn't match anything. However, if u change the match expression to [] then it matches the two sentences.
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, parse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation document = new Annotation("This is sent 1. And here is sent 2");
pipeline.annotate(document);
List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
Env env = TokenSequencePattern.getNewEnv();
env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE);
env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE);
TokenSequencePattern pattern = TokenSequencePattern.compile(env,"[ { word:\"sent\" } ]");
TokenSequenceMatcher matcher = pattern.getMatcher(sentences);
while ( matcher.find() ) {
System.out.println( matcher.group() );
}
Thank you!
List<CoreLabel> tokens =
document.get(CoreAnnotations.TokensAnnotation.class);
TokenSequencePattern pattern= TokenSequencePattern.compile("[ {
word:\"sent\" } ]");
TokenSequenceMatcher matcher = pattern.getMatcher(tokens);
while (matcher.find())
{
String matchedString = matcher.group();
List<CoreMap> matchedTokens = matcher.groupNodes();
System.out.println(matchedString + " " + matchedTokens);
}

Using Matcher to extract URL domain name

static String AdrPattern="http://www.([^&]+)\\.com\\.*";
static Pattern WebUrlPattern = Pattern.compile (AdrPattern);
static Matcher WebUrlMatcher;
WebUrlMatcher = WebUrlPattern.matcher ("keyword");
if(WebUrlMatcher.matches())
String extractedPath = WebUrlMatcher.group (1);
Considering above codes, My aim is to extract the domain name from the URL and dismiss the rest. But the trouble is that, first of all, if the URL has deeper path, it will not ignore it and second, it does not work for all URL with .com extension.
For example, if the URL is http://www.lego.com/en-us/technic/?domainredir=technic.lego, the result will not be lego but lego.com/en-us/technic/?domainredir=technic.lego.
Use
static String AdrPattern="http://www\\.([^&]+)\\.com.*";
^^ ^
You escaped the final dot, and it was treated as a literal, and matches could not match the entire string. Also, the first dot must be escaped.
Also, to make the regex a bit more strict, you can replace the [^&]+ with [^/&].
UPDATE:
static String AdrPattern="http://www\\.([^/&]+)\\.com/([^/]+)/([^/]+)/([^/]+).*";
static Pattern WebUrlPattern = Pattern.compile (AdrPattern);
static Matcher WebUrlMatcher = WebUrlPattern.matcher("http://www.lego.com/en-us/technic/?domainredir=technic.lego");
if(WebUrlMatcher.matches()) {
String extractedPath = WebUrlMatcher.group(1);
String extractedPart1 = WebUrlMatcher.group(2);
String extractedPart2 = WebUrlMatcher.group(3);
String extractedPart3 = WebUrlMatcher.group(4);
}
Or, with \G:
static String AdrPattern="(?:http://www\\.([^/&]+)\\.com/|(?!^)\\G)/?([^/]+)";
static String AdrPattern="http://www\\.([^/&]+)\\.com/([^/]+)/([^/]+)/([^/]+)";
static Pattern WebUrlPattern = Pattern.compile (AdrPattern);
static Matcher WebUrlMatcher = WebUrlPattern.matcher("http://www.lego.com/en-us/technic/?domainredir=technic.lego");
int cnt = 0;
while(WebUrlMatcher.find()) {
if (cnt == 0) {
String extractedPath = WebUrlMatcher.group(1);
String extractedPart = WebUrlMatcher.group(2);
cnt = cnt + 1;
}
else {
String extractedPart = WebUrlMatcher.group(2);
}
}

How to parse this JSON in C++ Builder?

I have the following JSON:
{"test1":"1", "test2": {"test21":"21", "test22":"22"}}"
but I'm having troubles parsing it. Actually, I'm somehow trying to read "test21" but don't know how to reach it. I tried this but it's not good:
UnicodeString myJSON = "{\"test1\" :\"1\",\"test2\":{\"test21\":\"21\",\"test22\":\"22\"}}";
TJSONObject *JSON = (TJSONObject*)TJSONObject::ParseJSONValue(myJSON);
TJSONValue *test2 = (TJSONValue*)JSON->Get("test2");
//TJSONString* test21 = (TJSONString*)test2->Get("test21");
TJSONObject *JSON = (TJSONObject*)TJSONObject::ParseJSONValue(myJSON);
TJSONPair *pair = JSON->Get("test2");
TJSONObject *jsonObj = (TJSONObject*) pair->JsonValue;
TJSONPair *test21 = jsonObj->Get("test21");
String value = test21->JsonValue->ToString();

Split String Using Regex in c#

I have a file with the following content
aaaaa(fasdfiojasdlfkj)
213.df(fasdfsadffdfsd)
53434534535(oipowerier)
2.3.*12.4(asdfrwer)
i would like to have a list like this eventually,
List<string[]> sList = new List<string[]>();
sList[0] = new string[]{"aaaaa", "fasdfiojasdlfkj"};
sList[1] = new string[]{"213.df", "fasdfsadffdfsd"};
sList[2] = new string[]{"53434534535", "oipowerier"};
sList[3] = new string[]{"2.3.*12.4", "asdfrwer"};
You can do this without regex:
var result = stringlist.ConvertAll(x =>x.Split(new char[] {'(',')'},
StringSplitOptions.RemoveEmptyEntries));
You don't need Regex for this - string.Split will be enough.
If you use it per line:
List<string[]> sList = new List<string[]>();
foreach(var line in fileLines)
{
sList.Add(line.Split(new Char[]{ '(', ')'},
StringSplitOptions.RemoveEmptyEntries));
}
List<string[]> sList = new List<string[]>();
MatchCollection matches = Regex.Matches(yourtext, #"([^\(]+)\(([^\)]+)\)");
foreach(Match mymatches in matches)
{
//get the data
string firststring = mymatches.Groups[1].Value;
string secondstring = mymatches.Groups[2].Value;
sList.Add(new string[] {firststring, secondstring});
}
not tested though....