This repository was archived by the owner on May 9, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstring-utils.js
More file actions
85 lines (83 loc) · 3.05 KB
/
Copy pathstring-utils.js
File metadata and controls
85 lines (83 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class StringUtils {
getInfo() {
return {
id: 'benstringutils',
name: 'String Utils',
blocks: [
{
opcode: 'startsWithScratch',
blockType: Scratch.BlockType.BOOLEAN,
text: '[string1] starts with [string2]',
arguments: {
string1: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'apple'
},
string2: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'app'
}
}
},
{
opcode: 'replaceScratch',
blockType: Scratch.BlockType.REPORTER,
text: 'replace in [string1], [string2] with [string3]',
arguments: {
string1: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hi, how are you?'
},
string2: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hi'
},
string3: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hello'
}
}
},
{
opcode: 'cutScratch',
blockType: Scratch.BlockType.REPORTER,
text: 'cut in [string1] from [start] to [end]',
arguments: {
string1: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hi, how are you?'
},
start: {
type: Scratch.ArgumentType.STRING,
defaultValue: '1'
},
end: {
type: Scratch.ArgumentType.STRING,
defaultValue: '4'
}
}
}
]
};
}
startsWithScratch(args) {
return args.string1.startsWith(args.string2);
}
replaceScratch(args) {
return args.string1.replace(args.string2, args.string3);
}
cutScratch(args) {
var x = (Number(args.start) - 1);
var y = (Number(args.end) - 1);
var str = args.string1;
if (x != null && y != null) {
if (x < 0 || y < 0 || x >= str.length || y >= str.length || x > y) {
return "Invalid Inputs";
}
return str.slice(0, x) + str.slice(y + 1);
} else {
return "var is empty"
}
}
}
Scratch.extensions.register(new StringUtils());