mercredi 1 avril 2015

Java String.replace() - replaces more than just the substring I specify?

As per this CodingBat problem I am trying to do the following:



Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.



My code:



public String withoutX(String str) {
if (str.startsWith("x")) {
str = str.replace(str.substring(0, 1), "");
}
if (str.endsWith("x")) {
str = str.replace(str.substring(str.length()-1), "");
}
return str;
}


This code replaces ALL the x characters in the string, rather than just the first and last. Why does this happen, and what would be a good way to solve it?


I have json that looks like the following



{
"tiles":[
{
"header":"Manual",
"subheader":"Login",
"summary1_label":"",
"summary2_label":"",
"summary3_label":"",
"summary4_label":"",
"summary1_value":"",
"summary2_value":"",
"summary3_value":"",
"summary4_value":"",
"iconUrl":"http://ift.tt/1C39tpD"
},
{
"header":"NewUser",
"subheader":"Login",
"summary1_label":"",
"summary2_label":"",
"summary3_label":"",
"summary4_label":"",
"summary1_value":"",
"summary2_value":"",
"summary3_value":"",
"summary4_value":"",
"iconUrl":"http://ift.tt/1OVVAUg"
},
{
"header":"Facebook",
"subheader":"Login",
"summary1_label":"",
"summary2_label":"",
"summary3_label":"",
"summary4_label":"",
"summary1_value":"",
"summary2_value":"",
"summary3_value":"",
"summary4_value":"",
"iconUrl":"http://ift.tt/1C39tpF"
}
]
}


I cast this as a JsonObject and send it to a reformating method. in that method i do the following



public ArrayList<String[]> formatHttpResponse_SummaryTile(JSONObject json) throws JSONException {
ArrayList<String[]> arrayList_summary = new ArrayList<String[]>();
JSONArray tilesArray = json.getJSONArray("tiles");
//JSONObject allTilesData = json.getJSONObject("tiles");
for (int i=0; i<tilesArray.length(); i++)
{
//JSONObject thisJsonObject = tilesArray.getJSONObject(i);
JSONArray thisJsonArray = tilesArray.getJSONArray(i);
String[] thisStringArray = new String[thisJsonArray.length()];
for (int j=0; j<thisJsonArray.length(); j++)
{
thisStringArray[j]=thisJsonArray.getString(j);
}
arrayList_summary.add(thisStringArray);
}
return arrayList_summary;
}


As you can see i create an array list then cycle through my jsonObjects 3 children. Now the problem is that i want to convert the children to String[][ but my code bugs out at JSONArray thisJsonArray = tilesArray.getJSONArray(i); as it isnt a jsonArray.


Any idea how i can get the children above into 3 string arrays all added to a arrayList Is there a more efficent, straight cast i can do?


Counting String objects created by Java code

How many String objects have been created by the following code?



String x = new String("xyz");
String y = "abc";
x = x + y;


I have seen on another site some say that this line of code creates a 3 objects and some says this line of code create a 4 objects. So let me know how many objects created after this line of code executes.


How do I convert a deliminated file in base - R into seperate columns without creating a list?

I currently do this using the following code

write(unlist(fieldList),"c:/temp/test.txt") temp1<-cbind(fieldList,read.delim("c:/temp/test.txt",sep=" ",header=FALSE))


but this is slow because it obliges the app to write data to temp file on disk.


I have tried strsplit function and "qdap" library but they both create lists. I need to be able to manipulate the columns separately


NewColumn <- substr(temp1[,5],1,1)


PHP how to count the length of each word in the text file

i need some help. how to count the length of each word in the text file using PHP.


for example. there is the test.txt. and the contain is " hello everyone, i need some help." how to output the text and then count the length of each word,like:


array



hello => 5
everyone => 8
i => 1
need => 4
some => 4
help => 4


i just start to learn php. so please explain the detail about the code what you write.


many thanks


split String in java from a list

Values in my list(collection) list[a,b,c,d]


Here is my String "select ?,? from table where ?=?"


I want to fill every question mark with these values in order and in the last result should be "select a,b from table where c=d


Python putting r before unicode string variable

For static strings, putting an r in front of the string would give the raw string (e.g. r'some \' string'). Since it is not possible to put r in front of a string variable, what is the minimal approach to dynamically convert a string variable to its raw form? Should I manually substitute all backslashes with double backslashes?



str_var = u"some text with escapes e.g. \( \' \)"
raw_str_var = ???